Build Your Own AI Assistant Part 1 - Creating the Assistant
116316 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
111570 Views
Control Arduino with Python using Firmata / PyFirmata
86865 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
56603 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
52712 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
51798 Views
Weather Station Display
Pi 10 Inch Mini-rack
Installing and Using DeepSeek-R1:1.5 on a Raspberry Pi with Docker
Gamepad & BurgerBot
Level Up your CAD Skills
Operation Pico
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
KevsRobots Learning Platform
55% Percent Complete
By Kevin McAleer, 4 Minutes
Welcome to Lesson 10 of the Raspberry Pi Pico with MicroPython - GPIO Mastery course. In this lesson, we will learn how to use temperature sensors with the Raspberry Pi Pico and MicroPython.
Raspberry Pi Pico with MicroPython - GPIO Mastery
In this lesson, you will learn:
After you have completed this lesson, you will know how to use temperature sensors with the Raspberry Pi Pico and MicroPython. You will be able to connect a temperature sensor to the Pico, write MicroPython code to read temperature sensor values, and understand the basics of temperature sensor operation.
To follow this lesson, you will need:
Temperature sensors are electronic devices that measure the temperature of the surrounding environment. There are many different types of temperature sensors, including thermistors, thermocouples, and digital temperature sensors.
To connect a temperature sensor to your Raspberry Pi Pico board, follow these steps:
To read temperature sensor values using MicroPython, you will need to use a library specific to your temperature sensor. Here is an example of reading temperature values from a DS18B20 digital temperature sensor using the onewire and ds18x20 modules:
onewire
ds18x20
from machine import Pin import onewire, ds18x20 # Set up the 1-wire bus one_wire_bus = Pin(4) ds_sensor = ds18x20.DS18X20(onewire.OneWire(one_wire_bus)) # Scan for temperature sensors on the bus ds_sensor.scan() # Read the temperature value ds_sensor.convert_temp() temperature = ds_sensor.read_temp(roms[0]) # Print the temperature value print("Temperature: {:.2f} degrees C".format(temperature))
import machine import dht # Initialize the DHT11 sensor on pin 15 d = dht.DHT11(machine.Pin(15)) # Read the temperature and humidity values from the sensor d.measure() # Print the temperature and humidity values print("Temperature: {} C".format(d.temperature())) print("Humidity: {} %".format(d.humidity()))
In this example code, we first import the machine module and the dht module, which provides support for the DHT11 sensor.
machine
dht
We then initialize the DHT11 sensor by creating an instance of the dht.DHT11 class with the GPIO pin number of the data pin connected to the sensor. In this case, we have connected the data pin of the sensor to GPIO pin 15.
dht.DHT11
Next, we use the measure() method of the DHT11 class to read the temperature and humidity values from the sensor. This method updates the internal state of the DHT11 instance with the latest sensor data.
measure()
DHT11
Finally, we print the temperature and humidity values using the temperature() and humidity() methods of the DHT11 instance, respectively.
temperature()
humidity()
Note that the DHT11 sensor requires a pull-up resistor on the data pin, so it is recommended to use a breadboard or other prototyping board to connect the sensor to the Raspberry Pi Pico. Also, the DHT11 sensor is known to be relatively slow to respond to commands, so it may take several seconds to read the temperature and humidity values after calling the measure() method.
In this lesson, you learned how to use temperature sensors to measure the temperature in your environment, and how to read temperature sensor values using MicroPython. You can use this knowledge to create a variety of projects with your Raspberry Pi Pico board.
< Previous Next >