Raspberry Pi Temperature and Light Sensor

Design

Required

2 x DS18B20, 2 x 4.7k resistor

The DS18B20 is a rather useful sensor because you can read more than one of them using the same GPIO pin. The Raspberry Pi is able to recognise the input from each separate sensor. This system is called “1-wire”, although the sensor still needs power and GND connections.

Breadboarding Basics.

The way a breadboard works is like this…

There are red and blue rails for power (+ve, red) and ground (-ve, blue). All wires plugged into those rails are connected together, as shown by the red and blue wires on the left hand side. Most breadboards have two sets of power rails, allowing you to power different parts of a circuit with different voltages.

Raspberry Pi Temperature and Light SensorOn the main part of the breadboard you have rows of connections with a gap in the middle. The two sides are not connected to each other. Anything you connect to a row on one side is connected to all the other wires in that row on that side (as shown by yellow and green wires).

So if you plug something into position A1, it’s connected to B1, C1, D1 and E1 as well, but it’s not connected to the other side (F1-J1).

Anything you plug into F3 is connected to G3, H3, I3 and J3.

The DS18B20 sensor has three pins which are connected as follows:

  • Pin 1 connects to GND
  • Pin 2 connects to GPIO4 on the Raspberry Pi
  • Pin 3 connects to the 3.3V supply (3V3) from the Pi

The circuit is connected as in the diagram. A 4.7kO resistor is connected across pins 2 and 3 of the sensor. GPIO4 connects to sensor pin 2. Sensor pin 1 connects to GND. Sensor pin 3 connects to 3V3.

The 3V3 and GND wires could be connected directly to the sensor pins, but we’re using the breadboard power (red) and GND (blue) rails because this makes it easier to add more sensors later.

Reading the temperature with the Raspberry Pi

Check you circuit is wired correctly and the Raspbian SD card is in the Raspberry Pi, then power up the Pi.

Login, using pi as the login id and raspberry as password, then type…

sudo python dstemp.py

The first time you run, it will load the drivers, which takes a few seconds. Then you should see a temperature readout updated every couple of seconds.

Manipulating the sensor

Four easy ways to make the readout change:

• If you put your finger on the sensor, the temperature readout will slowly rise.

• If you breathe hot air on it, it will rise a bit faster.

• If you blast it with a hair-dryer it will go much higher, much faster, and stay high for longer.

• An ice cube wrapped in kitchen paper can quickly make the temperature readout go down.

Every once in a while the program reminds you how to exit (CTRL+C).
When you’ve finished with that, it’s time to add another sensor.

This could easily be used, with longer wires, to form the basis of a temperature control system or a weather station showing indoor and outdoor temperatures.

Add another sensor.

It’s best to do wiring with the Pi powered down, so shut it down by typing

sudo halt

After about 10-20 seconds the LEDs on the Pi stop blinking and you can pull out the power lead.

Now we need to add another sensor to our breadboard circuit. No need to change what we already did, we’re just adding a sensor, a resistor and three wires.

Notice that a wire connects the middle pin of each sensor. These are both read from the same pin (GPIO4) on the Raspberry Pi.

Once your circuit is correct, power up the Pi again, Login, using pi as the login id and raspberry as password, then type…

Raspberry Pi Temperature and Light Sensor

sudo python dstemp.py

This time, after loading the drivers, it should show you the readout from both sensors.

You can manipulate them as before to prove that they are working independently. You might occasionally get the odd spurious reading (e.g. 0.06) but usually it will only be a one-off.

Add an indicator ledNow let’s add an indicator led that will light up when sensor 1 reads a temperature above 25 °°C (should be possible with a few hot breaths).

It’s advisable to power down the pi again at this point, as before.

We need to add:

• a female to male jumper wire from GPIO 24 to led positive (long leg)

• an LED

• a 47 O resistor from led negative (flat side) to the GND rail (blue)

 

For more detail: Raspberry Pi Temperature and Light Sensor


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top