The Raspberry Pi Pico is the perfect approach to get into microcontrollers. Ranging from $4, the board is affordable and straightforward to work with. The low value and ease of use means we will simply drop them right into a venture with out fearing the worst for our pockets.
On this how-to, we are going to use a Raspberry Pi Pico to seize dwell temperature information utilizing a DS18B20. This sensor is available in many kinds, from a naked transistor chip, to a water-proof cable. We’ll be utilizing the latter model, which could be partially submerged in a liquid to watch the temperature. Our venture will take a temperature studying and utilizing a conditional check in MicroPython it’s going to set off an LED to flash if the temperature goes beneath 20 levels Celsius.
For This Mission You Will Want
Constructing The Circuit
The circuit is made up of two components. The DS18B20 temperature sensor, and the LED. We’ll break them down into two sections.
The DS18B20 has three connections to the Raspberry Pi Pico.
Raspberry Pi Pico | Wire Colour | DS18B20 |
---|---|---|
3V3 | Pink | VDD |
GPIO26 | Yellow | Knowledge |
GND | Black | GND |
All three connections are made into the breadboard, and the wires are used to connect with the Pico. Between the info and 3V3 pins (yellow and crimson), there’s a 4.7K Ohm resistor which is used to tug the info pin excessive utilizing the three.3V equipped. This maintains a gradual connection between the info pin and Pico.
The LED has solely two connections to the Raspberry Pi Pico.
Raspberry Pi Pico | Wire Colour | LED |
---|---|---|
GPIO15 | Pink | Anode (Lengthy leg) |
GND | Black | Cathode (Brief leg) |
The LED has a 330 Ohm resistor on the cathode leg, inline with GND. This reduces the quantity of present that the LED can devour.
Writing the Code
1. Observe this information to obtain and set up the most recent MicroPython launch to your mannequin of Raspberry Pi Pico. Observe the steps as much as and together with connecting the Raspberry Pi Pico to Thonny.
2. Create a brand new clean venture in Thonny.
3. Import three modules of pre-written code. First is onewire, a module that allows the Pico to speak to the one-wire interface of the DS18B20. Subsequent is ds18x20, a module that interprets the sensor information from the DS18B20 and gives us with human readable information. Lastly we import time which is used to tempo our venture code.
import onewire, ds18x20, time
4. From the Machine module import the Pin class. It will allow our code to work together with the elements related to the GPIO.
from machine import Pin
5. Create two objects, SensorPin and alert. SensorPin is the GPIO pin used to attach the info pin from the DS18B20 to the Pico. Alert is the GPIO pin that connects to the anode (lengthy leg) of the LED.
SensorPin = Pin(26, Pin.IN)
alert = Pin(15, Pin.OUT)
6. Create an object, sensor and use it to inform the ds18x20 module the place to seek out our DS18B20 temperature sensor. This line additionally makes use of the onewire module, as that’s the protocol that the sensor makes use of for connection.
sensor = ds18x20.DS18X20(onewire.OneWire(SensorPin))
7. Create an object, roms and use the sensor object to scan the interface to seek out our DS18B20 temperature sensor. All one-wire units, akin to our DS18B20, have a singular registration quantity saved in ROM that we have to determine earlier than they can be utilized.
roms = sensor.scan()
8. Use some time True loop to run the following traces of code in a by no means ending loop.
whereas True:
9. Set the temperature studying to make use of Celsius. Be aware that the code throughout the whereas True loop is indented to point out that it belongs within the loop.
sensor.convert_temp()
10. Add a two second pause to the code. This provides the DS18B20 time to settle earlier than we take a studying.
time.sleep(2)
11. Use a for loop to iterate via the returned listing of ROMs. As we solely have one DS18B20 on the one-wire interface, just one ROM shall be saved within the listing that we iterate over.
for rom in roms:
12. Create an object, temperature, and use it to retailer the output of studying the DS18B20 sensor. The output is wrapped in a spherical() perform that can around the returned information to 1 decimal place.
temperature = spherical(sensor.read_temp(rom),1)
13. Use a conditional check to verify the worth saved within the temperature object in opposition to a tough coded worth. On this instance, if the temperature is lower than or equal to twenty Levels Celsius then the situation will consider as True, and the following part of indented code will run.
if temperature <= 20:
14. Print a message to the Python Shell that warns the person that the mood is beneath 20 C and embrace the temperature worth within the sentence.
print("Warning the temperature is",temperature,"C")
15. Use a for loop to blink the LED 10 occasions, with a 0.5 second delay.
for i in vary(10):
alert.toggle()
time.sleep(0.5)
16. Create an else situation that prompts if the temperature is bigger than 20 C. This situation will print the present temperature earlier than ending.
else:
print(temperature,"C")
17. Add a 5 second pause earlier than ending the loop and returning again to the beginning of the loop.
time.sleep(5)
18. Save the code to the Raspberry Pi Pico as TemperatureMonitor.py.
19. Click on on the Run icon to begin the code. After a brief pause the temperature particulars will seem within the Python shell. If the temperature is beneath 20 C, the LED will blink 5 occasions to warn us.
Full Code Itemizing
import onewire, ds18x20, time
from machine import Pin
SensorPin = Pin(26, Pin.IN)
alert = Pin(15, Pin.OUT)
sensor = ds18x20.DS18X20(onewire.OneWire(SensorPin))
roms = sensor.scan()
print(roms)
whereas True:
sensor.convert_temp()
time.sleep(2)
for rom in roms:
temperature = spherical(sensor.read_temp(rom),1)
if temperature <= 20:
print("Warning the temperature is",temperature,"C")
for i in vary(10):
alert.toggle()
time.sleep(0.5)
else:
print(temperature,"C")
time.sleep(5)