discovering the raspberry pi

Tag Archives: code

It has been a while since I last wrote. Too busy at work and no time to sit down and tinker… I’ve done some reading in the past couple of weeks, because I want to extend my Raspberry Pi with some additional IO ports for my garden project, especially some analog ports would be welcome.

In my garden project I want to be able to use the Raspberry Pi for the following tasks:

– operate the pool pump
– detect wether the filter is overflowing
– measure the water temperature
– open and close the garage door
– monitor the status of the garage door (open / closed)
– make time lapse photo’s of the garden
– turn the garden lights on / off
– detect day / night
– and anything else I can possibly think off

My journey starts with adding ports to the Raspberry Pi. I figured, instead of adding a ATTiny 85 I decided to buy for €9,50 a Arduino Uno copy from China, which is much more versatile than the ATTiny 85. I figured that I can make the Raspberry Pi and Arduino Uno talk via I2C.

All I need would be some code for the Arduino Uno to accept commands from the Raspberry PI via I2C. I want to be able to set all pins individually to INPUT or OUTPUT or INPUT_PULLUP. Secondly, I must be able to set the pins HIGH or LOW. Also I want to be able to use PWM on the PWM capable digital pins (pin 3, 5, 6, 9, 10, 11) and finally, I must be able to read the values from the digital and analog pins.

So, with barely any programming skills, I wrote some code that does exactly this. The Arduino Uno Slave code looks like this:

// 1) to set the pinMode write a message with 7 characters on I2C bus to the arduino// first character = S for set pinMode// second & third character are pin ID 00 – 13 for digital pins & A0 – A3 for analog pins// fourth character is to set the mode I for INPUT, O for OUTPUT, P for INPUT_PULLUP// character 5,6,7 are not used, set to 000

// 2) to turn the pin on or off write a message with 7 characters on I2C bus to the arduino// first character = W for digitalWrite// second & third character are pin ID 00 – 13 for digital pins & A0 – A3 for analog pins// fourth character is to turn off or on H for HIGH and L for LOW// character 5,6,7 are not used, set to 000

// 3) to turn use PWM write a message with 7 characters on I2C bus to the arduino// first character = A for analogWrite// second & third character are pin ID 00 – 13 for digital pins & A0 – A3 for analog pins// forth character is not used, set to X// fifth – seventh character are used to write the PWM cycle (000-255)

// 4) to get a status with pin readings send Wire.requestFrom(device, #chars = 30)// the arduino will send back 30 chars // char 1-14 for each digital pin 1 = on 0 = off// char 15-18 for reading of A0, 1000 is added to the A0 reading in order to guarantee a 4 digit reading, subtract 1000 to get the proper reading// char 19-22 for reading of A1, 1000 is added to the A0 reading in order to guarantee a 4 digit reading, subtract 1000 to get the proper reading// char 23-26 for reading of A2, 1000 is added to the A0 reading in order to guarantee a 4 digit reading, subtract 1000 to get the proper reading// char 27-30 for reading of A3, 1000 is added to the A0 reading in order to guarantee a 4 digit reading, subtract 1000 to get the proper reading

// Created 11 July 2013

// This example code is in the public domain.

#include <Wire.h>

void setup(){ int arduinoI2CAddress = 33; // set the slave address for the Arduino on the I2C buss

// 1) to set the pinMode write a message with 7 characters on I2C bus to the arduino// first character = S for set pinMode// second & third character are pin ID 00 – 13 for digital pins & A0 – A3 for analog pins// fourth character is to set the mode I for INPUT, O for OUTPUT, P for INPUT_PULLUP// character 5,6,7 are not used, set to 000

// 2) to turn the pin on or off write a message with 7 characters on I2C bus to the arduino// first character = W for digitalWrite// second & third character are pin ID 00 – 13 for digital pins & A0 – A3 for analog pins// fourth character is to turn off or on H for HIGH and L for LOW// character 5,6,7 are not used, set to 000

// 3) to turn use PWM write a message with 7 characters on I2C bus to the arduino// first character = A for analogWrite// second & third character are pin ID 00 – 13 for digital pins & A0 – A3 for analog pins// forth character is not used, set to X// fifth – seventh character are used to write the PWM cycle (000-255)

// 4) to get a status with pin readings send Wire.requestFrom(device, #chars = 30)// the arduino will send back 30 chars // char 1-14 for each digital pin 1 = on 0 = off// char 15-18 for reading of A0, 1000 is added to the A0 reading in order to guarantee a 4 digit reading, subtract 1000 to get the proper reading// char 19-22 for reading of A1, 1000 is added to the A0 reading in order to guarantee a 4 digit reading, subtract 1000 to get the proper reading// char 23-26 for reading of A2, 1000 is added to the A0 reading in order to guarantee a 4 digit reading, subtract 1000 to get the proper reading// char 27-30 for reading of A3, 1000 is added to the A0 reading in order to guarantee a 4 digit reading, subtract 1000 to get the proper reading