That was the final result of our effort today. It is a thermometer that will let you know how warm it is in your room, by using an RGB LED strip placed in an acrylic container, that is connected to a temperature sensor to read the temperature. And we'll be using PICO to get this project to life.
Step 1: Components
- PICO, available on mellbell.cc ($17)
- 1 meter RGB LED strip
- 3 TIP122 Darlington transistor, a bundle of 10 on ebay ($3.31)
- 1 PCA9685 16-channel 12-bit PWM driver, available on ebay ($2.12)
- 12v power source
- 3 1k ohm resistors, a bundle of 100 on ebay ($0.99)
- A breadboard, available on ebay ($2.30)
Male - female jumper wires, a bundle of 40 on ebay ($0.95)
Step 2: Powering the RGB Strip With Transistors and a Power Source
LED strips are flexible circuit boards that are populated with LEDs. They are used in many ways, as you can use them in your house, your car, or bike. You can even create cool RGB wearables using them.
So, how do they work? It is actually pretty simple. All of the LEDs in the LED strip are connected in parallel, and they act like one huge RGB LED. And to run it, you simply need to connect the strip to a 12v high current power source.
To control the LED strip with a microcontroller, you need to separate the power source from the control source. Because the LED strip needs 12v, and our microcontroller can't offer this much output voltage, and that is why we connect an external 12v high current power source, while sending the control signals from our PICO.
Also, the current draw of each RGB cell is high, as every single LED in it - the red, green, and blue LEDs - needs 20mA to operate, which means that we need 60mA to run light up a single RGB cell. And that is very problematic, because our GPIO pins can only supply the maximum of 40mA per pin, and connecting the RGB strip to PICO directly will burn it, so please don't do it.
But, there is a solution, and it is called the Darlington Transistor which is a pair of transistors that has very high current gain, which will help us boost our current to fill our needs.
Let's learn more about current gain first. Current gain is a property of transistors that means that the current passing through the transistor will be multiplied by it, and its equation looks like this:
load current = input current * transistor gain.
This is even stronger in a Darlington transistor, because it is a pair of transistors not a single one, and their effects are multiplied by each other, giving us massive current gains.
We will now connect the LED strip to our external power source, the transistor, and of course our PICO.
- Base (transistor) → D3 (PICO)
- Collector (transistor) → B (LED strip)
- Emitter (transistor) → GND
- +12 (LED strip) → +12 (power source)
Don't forget to connect PICO's GND to the power sources ground.
Step 3: Controlling the Colors of the RGB LED Strip
We know that our PICO has a single PWM pin (D3) which means that it can't natively control our 16 LEDs. This is why we are introducing the PCA9685 16-channel 12-bit PWM I2C module, which lets us expand PICO's PWM pins.
First of all, what is I2C?
I2C is a communication protocol that involves only 2 wires to communicate with one or more devices by addressing the device's address and which data to send.
There are two types of devices: The first one is the master device, which is the one responsible for sending data, and the other is the slave device, which receives the data. Here are the pin outs of the PCA9685 module:
- VCC → This is the power for the board it self. 3-5v max.
- GND → This is the negative pin, and it must be connected to the GND to complete the circuit.
- V+ → This is an optional power pin that will supply power to servos if you have any of them connected to your module. You can leave it disconnected if you are not using any servos.
- SCL → Serial clock pin, and we connect it to the SCL of PICO.
- SDA → Serial Data pin, and we connect it to the SDA of PICO.
- OE → output enabled pin, this pin is active LOW, when the pin is LOW all the outputs are enabled, when it’s HIGH all the outputs are disabled. And this optional pin is used to quickly enable or disable the module's pins.
There are 16 ports, each port has V+, GND, PWM. Each PWM pin is runs completely independently, and they are set up for servos but you can use them for LEDs easily. Each PWM can handle 25mA of current so be careful.
Now that we know what our module's pins and what it does, lets use it to increase the number of PICO's PWM pins, so that we can control our RGB LED strip.
We are going to use this module along with TIP122 transistors, and this is how you should connect them to your PICO:
- VCC (PCA9685) → VCC (PICO).
- GND (PCA9685) → GND.
- SDA (PCA9685) → D2 (PICO).
- SCL (PCA9685) → D3 (PICO).
- PWM 0 (PCA9685) → BASE (first TIP122).
- PWM 1 (PCA9685) → BASE (second TIP122).
- PWM 2 (PCA9685) → BASE (third TIP122).
Don't forget to connect PICO's GND with the power supply's GND. And make sure NOT to connect the PCA9685 VCC pin with the power supply's +12 volts or it will get damaged.
Step 4: Control the RGB LED Strip Color Depending on the Sensor's Reading
This is the last step in this project, and with it our project will transform from being "stupid" to being smart and having the ability to behave depending on its environment. To do that we are going to connect our PICO with the LM35DZ temperature sensor.
This sensor has an analog output voltage that depends on the temperature around it. It starts at 0v corresponding with 0 Celsius, and the voltage increases by 10mV for each degree above 0c. This component is very simple and only has 3 legs, and they are connected as follows:
- VCC (LM35DZ) → VCC (PICO)
- GND (LM35DZ) → GND (PICO)
- Output (LM35DZ) → A0 (PICO)
Step 5: The Final Code
Now that we have everything connected to our PICO, lets start programming it so that the LEDs change color depending on the temperature.
For this, we need the following:
- A const. variable named "tempSensor" with the value A0 which receives its reading from the temperature sensor.
- An integer variable named "sensorReading" with initial value 0. This is the variable that will save the raw sensor reading.
- A float variable named "volts" with the initial value 0. This is the variable that will save the converted sensor raw reading value to volts.
- A float variable named "temp" with initial value 0. This is the variable that will save the converted sensor volts readings and convert it into temperature.
- An Integer variable named "mapped" with initial value 0. This will save the PWM value that we map the temp variable into, and this variable controls the LED strip color.
Using this code, PICO will read the temperature sensor's data, convert it into volts, then into Celsius, and finally it maps the Celsius degree into a PWM value that can be read by our LED strip, and that is exactly what we need.
Step 6: You Are Done!
We also made an acrylic container for the LED strip to make it stand up in a nice way. You can find the CAD files here if you want to download them.
You now have an awesome looking LED thermometer that automatically tells you the temperature when you look at it, which is pretty convenient to say the least :P