Saturday, 1 June 2013

Connecting temperature and motion sensors to Raspberry Pi

I will connect a temperature sensor and a motion sensor directly to Raspberry Pi (Model B), both sensors are digital and one-wire. I will also use two LEDs as signals. When the temperature raises or motion detected those will flash.

Below are the links to the actual parts I used. Eventually these links may stop working, but all the parts are very common and easily obtainable.
  1. Yellow LED
  2. Red LED
  3. Breadboard
  4. Jumper wires M/M
  5. Jumper wires M/F 
  6. Two 100 Ohm resistors
  7. One 4.7k Ohm resistor
  8. One 10k Ohm resistor
  9. DS18B20 digital temperature sensor
  10. SparkFun SE-10 PIR motion sensor

Here is the circuit



The big red square are Raspberry Pi (Model B) pins. The pins differ on Model A. Two smaller red squares are sensors - temperature and motion. Four tiny red squares are resistors. Plus two diodes (LEDs). Green lines are wires and green dots are wire junctions. Wire properly and it should work.

Calculating LED resistor: why 100 Ohm?


Ohm's Law:   I = V / R    →   R = V / I
  • I is the current measured in amperes, V is the voltage measured in volts, R is the resistance measured in ohms.
The board supplies 3.3V and a LED maximum current is 20 mAmps (according to manufacturer). Thus the safe current for the LED is around 12 mAmps.

R = 3.3 / 0.012 = 275 Ohm

Oh, the LED itself acts as a resistor of approximately 175 Ohm (measured manually), thus:
R = 275 - 175 = 100 Ohm

So, if we put a 100 Ohm resistor in a circuit with one LED, the current in the entire circuit will be 12 mAmps.

Pull-up resistor


A pull-up resistor connects sensor's power terminal with data terminal. Read more about pull-up resistors here. One sensor in the circuit is using 4.7k Ohm and another is using 10k Ohm pull-ups.

Wiring sensors and LEDs


Read manufacturer's data sheet to understand which of the sensor's terminals connects to power, data and ground. E.g. on my motion sensor the white wire was ground and the black wire was alarm (data).

To wire and enable DS18B20 temperature sensor please use this guide. Follow the guide and the sensor will be connected to the GPIO4 pin exactly as on my circuit. Here is the data sheet.

Motion sensor connects in the similar manner to GPIO14 and uses 5V. It will send data directly to the GPIO14 pin. Here is the data sheet.

Wiring a LED is pretty simple but you may use this guide, it has pictures!

Python program


It checks room temperature every half a second and lights up one LED when the temperature is above 20 C (just breath on the sensor). Lights up another LED when it detects motion.  You may want to alter the temperature limit ans change the device directory (mine is 28-0000047b16ad) in the source code. Download link.

 And finally, a pretty picture

 


In my next post I will assemble the same circuit using Pi-Face.

1 comment: