Arduino + Raspberry= Weather station with webcam (Part One: the I2C link)

Hi geek boys and girls!

After few more technical and obscure posts, finally I give some (original? I don’t think so…but it’s funny!) creative idea for your *ware open source projects.

With this post I would like to start building a open source meteo weather station.

In order to do this task I think you could connect together, such as a great cup of Mojito 🙂 :

  • A set of sensors (i.e. humidity, barometric pressure, temperature)
  • One little camera (i.e. we could start with an inexpensive webcam…if the weather conditions would permit its use!)
  • One Arduino UNO (or one Intel Galileo ;-))
  • One Raspberry PI Model B
  • One GSM module, or more simply, one less expensive USB internet key (to access via internet to your station)

Ok, first question: why Raspberry AND Arduino (and not only one board)?

Well… I would like to use Arduino to manage all sensors (especially if they would be analog sensors, since Raspberry doesn’t have analog inputs), and I would like to use Raspberry to manage the webcam and the communication with the external world via the GSM module (since it has  high-level functiona).

So I will use Arduino as acquisition board and Raspberry as data collector and as webserver.

Raspberry and Arduino can talk to each other using some different approaches, but I would like to use the I2C protocol, because it’s very simple and very well supported by Raspbian distribution and by native Arduino libraries.

I found this interesting post by Peter Mount in order to safely connect Arduino (as slave) and Raspberry (as master) using I2C.

In order to activate the I2C bus on Raspberry Peter reports five step on Raspbian:

  1. Open /etc/modprobe.d/raspi-blacklist.conf and comment the line reporting  i2c-bcm2708 (so, I2C is removed from blacklist)
  2. Add i2c-dev to the /etc/modules in order to activate the I2C driver at boot
  3. Install i2c-tools using thje well known apt-get install command
  4. Add the “pi” user to the i2c group (using the adduser pi i2c command) in order to let the user “pi” can access to I2C
  5. Reboot the Raspberry board.

In order to phisically connect Arduino and Raspberry via I2C:

  1. Connect SDA Raspberry pin (GPIO0) to SDA Arduino UNO pin (Digital IO 4)
  2. Connect SCL Raspberry pin (GPIO1) to SCL Arduino UNO pin (Digital IO 5)
  3. Connect the ground pins fo the two boards

Remember that Raspberry uses 3.3V as base voltage and Arduino UNO uses 5V…so pay attention: OR if you use a voltage converter in the I2C connection OR you are sure that you are using Arduino as slave I2C device and Raspberry as master I2C device. The first one choice is the best…but I love the risks (the risk in this cas is to have a “Raspberry flambé” 😉 ).

Ok, let’s go ahead. I used a little analog temperature sensor (the one provided in all starter kits by Analog Devices! 😉 ) connected to Arduino UNO (in the Analog input A0)  and then I modified the code provided on the Peter Mount blog in order to read the ambient temperature using Arduino and send it to Raspberry via I2C bus.

This is the simple circuit I used (zoom to better view the links):


The code on Raspberry is the following (it’s directly taken from Peter Mount Blog, except for only one correction: I defined file as FILE* since declared as int raised a segmentation fault on fclose call….):

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>

// The Arduino board i2c address
#define ADDRESS 0x04

//For V1 Model B you need i2c-0
//For V2 you need i2c-1( my case I have a V2 Raspberry Pi! :-) )
static const char *devName = "/dev/i2c-1"; //)

int main(int argc, char** argv) {
 if (argc == 1) {
   printf("Supply one or more commands to send to the Arduino\n");

 printf("I2C: Connecting\n");
 FILE* file;
 if ((file = open(devName, O_RDWR)) < 0) {
   fprintf(stderr, "I2C: Failed to access %d\n", devName);

 printf("I2C: acquiring buss to 0x%x\n", ADDRESS);
 if (ioctl(file, I2C_SLAVE, ADDRESS) < 0) {
   fprintf(stderr, "I2C: Failed to acquire bus access/talk to slave 0x%x\n", ADDRESS);

 int arg;
 for (arg = 1; arg < argc; arg++) {
   int val;
   unsigned char cmd[16];
   if (0 == sscanf(argv[arg], "%d", &val)) {
     fprintf(stderr, "Invalid parameter %d \"%s\"\n", arg, argv[arg]);

   printf("Sending %d\n", val);
   cmd[0] = val;
   if (write(file, cmd, 1) == 1) {
    // As we are not talking to direct hardware but a microcontroller we
    // need to wait a short while so that it can respond.
    // 1ms seems to be enough but it depends on what workload it has
    char buf[1];
    if (read(file, buf, 1) == 1) {
      int temp = (int) buf[0];
      printf("Received %d\n", temp);

   // Now wait else you could crash the arduino by sending requests too fast
  return (EXIT_SUCCESS);


The code on Arduino UNO  is very simple and it is derived from the code taken from Peter Mount Blog:

#include <Wire.h>

#define SLAVE_ADDRESS 0x04

int number = 0; //command identifier (sent by Raspberry)
double temp; //variable used to store the temperature
const int sensorPin = A0; //pin where we read the temperature from the sensor
void setup() {
 // initialize i2c as slave
 // define callbacks for i2c 
void loop() {
 temp = GetTemp();
// callback for received data from I2C
void receiveData(int byteCount){
 while(Wire.available()) {
 number =;
 //"2" is the command sent by Raspberry Pi in order to have the 
 // temperature as answer on I2C from Arduino
 if(number==2) { 
     number = (int)temp; //Arduino in this case sends the integer value of temperature
// callback for sending data on I2C
void sendData(){

float GetTemp(){
 // read the value on AnalogIn pin 0 
 // and store it in a variable
 int sensorVal = analogRead(sensorPin);

 // convert the ADC reading to voltage
 double voltage = (sensorVal/1024.0) * 5.0;
 // convert the voltage to temperature in degrees C
 // the sensor changes 10 mV per degree
 // the datasheet says there's a 500 mV offset
 // ((voltage - 500mV) times 100)
 double temperature = (voltage - .5) * 100;
 return temperature;

Ok… we can now download the cvode on Arduino and we can compile the client software on Raspberry Pi using the good old command:

gcc -o TakeTemp main.c

So, once powered on Arduino and verified using the Raspberry command

i2cdetect -y 1

that Arduino is correctly detected with I2C address 0x04, write in the Raspberry command line

./TakeTemp 2

Note that “2” is the I2C command used to receive from Arduino the acquired temperature (see the Arduino code above).

…If all will work correctly, you will read as output the integer value of the sensor temperature, acquired by Arduino and sent to Raspberry via I2C!

Yeah geek guys (and obviously geek gals)…”this is one small step for the human race”, but it’s also a good start to develop an open source meteo station. 😉

And now… a good relax!:-) That’s all folks (for this time)! 😉

…Bye bye!

11 thoughts on “Arduino + Raspberry= Weather station with webcam (Part One: the I2C link)

  1. Pingback: Arduino + Raspberry= Weather station with webcam (Part Two: the meteo data acquisition) | ML's Garret Labs

  2. Pingback: Arduino and Android devices: the (not so) well known USB connection | ML's Garret Labs

  3. Pingback: Raspberry PI: A remote controlled relay using SMS messages | ML's Garret Labs

  4. Pingback: Arduino+Raspberry=Meteo station with webcam (Part Three: the webcam) | ML's Garret Labs

  5. Pingback: Arduino + Raspberry= Weather station with webcam (Part Four: the internet connection…. and the conclusion!) | ML's Garret Labs

  6. hallo,
    i Wanna to measure the Temp. and humidity with intel galileo board using sht15 sensor ,can you help me with Code , i think the Problem is With Library

    • Hi dear Ahmadto,
      thank you for your comment. Unfortunately I don’t have my garret 😦 a SHT15 sensor to execute tests.
      Plus, Galileo has a different CPU architecture from Arduino UNO, so, many times third party sensors libraries cannot work.
      I searched on internet …and I found your code 😉 here:
      I think the problem is tied to the library you recall in your sketch, which maps the Arduino registers (different from ones in Galileo).
      If you think it’s useful for you I could do some experiment with Galileo and your code (and your library for Arduino)… in order to have a correct compilation of your sketch, but without a real test with the sensor (because I haven’t 🙂 ). Let me know!
      …One question: Do you have the SHT15 breakout from Sparkfun ( or only the sensor (
      …My last opinion: I always use DHT11 or DHT21 sensors because they are very less expensive than SHT15 and they are more simple to use (in my honest opinion).
      Bye bye,

  7. Thanks for your help,
    I have DHT11 sensor , but i want to usei on Galileo Board , i know this sensor is one wire and doesnot work on Galileo.
    Do you have any opinion

    • Hi dear Ahmadto,
      thank you for your comment.
      Well… DHT11 doesn’t work natively with Galileo (it seems because it uses only one wire and Galileo is too slow to change from in to out -and vice versa- one pin.
      I’ve saw the thread posted on (where you partecipated). As soon as possible I will repeat the experiments executed by the user DinoT_Intel (=using two wires for DHT 11 and using his modified DHT11 library) and I will post here my results.
      So…keep in touch! 😉

  8. Thanks – but –
    With FILE* it would not compile
    with int I saw segmentation fault but got the numbers.

    I see you only send 1 byte to the Pi – so the a to d accuracy goes down by 4
    I am finding it hard to get code running to send 2 bytes to the Pi to get the full 1024
    Have you done that?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s