Build Your Own I2C Sensor

Since Raspberry Pi doesn’t have a built-in ADC (Analog to Digital converter) to read the voltage off from most of sensors, the best solution is to add I2C ADC chips and modules to your project.
Paweł Spychalski faced this problem while building his own weather station that is based on Raspberry Pi. It collects various data and displays them on dedicated web page and Android app. Every few months he tries to add a new sensor to it. Last time it was a daylight sensor. He added this sensor to his system by using ATtiny85 and it was connected via I2C bus.
ATtiny85 is a member of Atmel tiny AVR series which has 8-bit core and fewer features, fewer I/O pins, and less memory than other AVR series.
The Inter-integrated Circuit (I2C) Protocol is a protocol intended to allow multiple “slave” digital integrated circuits (“chips”) to communicate with one or more “master” chips. Like the Serial Peripheral Interface (SPI), it is only intended for short distance communications within a single device. Like Asynchronous Serial Interfaces (such as RS-232 or UARTs), it only requires two signal wires to exchange information.

I2C uses only two bidirectional open-drain lines, Serial Data Line (SDA) and Serial Clock Line (SCL), pulled up with resistors. Typical voltages used are +5 V or +3.3 V although systems with other voltages are permitted.
Most of developers use I2C to connect to sensors with the help of the Arduino “Wire” library or i2c-tools on the Pi, but it is rare to see someone that is actually building the I2C slave device. Paweł’s project uses Tiny Wires library, a slave-mode SPI and I2C library for AVR AT tiny Arduino projects.
This diagram shows how to build analog to digital converter using ATtiny85 and connect it to any device (Raspberry Pi, Arduino) using I2C bus. Here photo resistor has been used, but any analog meter will be fine: temperature, potentiometer, moisture…
Circuit Build Your Own I2C SensorATtiny85 directly connected to Raspberry Pi via I2C, photo resistor with 10kOhm pull down connected to ATtiny85 and signal LED.
For reading data you can use this code. AT tiny sends current measurement as two 8 bit value. First older bits, then younger 8 bits.

Wire. request From(0x13, 2); // request 2 bytes from slave device #0x13

int i =0;
unsigned int readout = 0;

while (Wire. Available()) { // slave may send less than requested
 byte c = Wire. Read(); // receive a byte as character

 if (i == 0) {
  readout = c;
 } else {
  readout = readout << 8;
  readout = readout + c;
 }

 i++;
}

Serial. Print(readout);

To do this project you need to use Arduino IDE 1.6.6., Tiny Wires library,ATtiny45/85 board, plus an 1MHz internal oscillator.

Read More: Build Your Own I2C Sensor

Leave a Comment

Your email address will not be published. Required fields are marked *