Delivery throughout Europe

Ordered before 16:00 = Shipped today

Fast delivery with DHL

XNUMX days return *


Country

Raspberry Pi Pico – Lesson 4: Raspberry Pi Read Pico analog sensor

In this project you will learn how to read an analog sensor with a Pico & MicroPython. This practical lesson is perfect for beginners who want to get started with the Thonny programming tool and MicroPython. Learn step by step how to do the Raspberry Pi Connect Pico to your computer, upload programs and read out an analog sensor, such as a potentiometer on the ADC of the Pico. Explore the world of analog sensors with this hands-on tutorial and take your first steps in using the Raspberry Pi Pico for sensory applications!

This manual covers: 

  • Level - Beginner 25% 25%
  • Duration 20 min 25% 25%
  • Costs €7,10 10% 10%

This is the fourth lesson from the introductory projects for the Raspberry Pi Pico. Before you start this lesson, we recommend that you first complete the previous lessons. You will find lesson 3 HERE, lesson 1 you will find HERE.

Step 2 – Install software on the Raspberry Pi Pico for the analog sensor

You can skip this step if you already did it in Lesson 1.
Around the Raspberry PiTo be able to program with MicroPython, we must first flash the firmware of the Pico.
This means that we provide the internal software that starts the pico with a special Python version.

Download firmware from the website below (uf2 file)
https://micropython.org/download/RPI_PICO/
https://micropython.org/download/RPI_PICO_W/
(Make sure you select the correct version of the firmware, the WiFi version and the non-WiFi version are different)
Press the white boot button on the pico and keep it pressed.
Plug the pico into a USB port on your computer. (Then you can release the button)
There will now be a drive letter, as accessible with a memory stick.
Copy the firmware file RPI_PICO_xxxxxx.uf2 to this drive
(pico will restart and the firmware will be installed)
Download and install Thonny on your PC.
(https://thonny.org/)

Select the MicroPython Interpreter below
Tools > Options > Interpreter > MicroPython (Raspberry Pi Pico)

 

Place the components as shown below on a breadboard.

The potentiometer (adjustable resistance) is 10K ohm.
The capacitor used is 0.1uF. This will make the read values ​​slightly more stable, but is not necessary for testing.

 

Step 3 – Programming the Raspberry Pi Pico to read the potentiometer

Now you can type in the program below in the Thonny interface.

from machine import ADC from time import sleep analog = ADC(26) # adc0 while True: value = int(analog.read_u16() ) # Below gives values ​​from 0 - 255 # value = int(analog.read_u16() /256) print("Analog value is : " + str(value)) sleep(1)

Explanation of the program:

  1. Import the ADC class (Analog digital converter) from the Python machine module
  2. Import the sleep class from the time module
  3. .
  4. Read Pin 26 adc0
  5. An endless loop is started here
  6. enter the read analog value from pin 26 (adc0) in the variable value
  7. comment about the example, the reading is accurate but will also fluctuate a lot.
    The capacitor will counteract part of this interference, another solution is on line 8
  8. Remove the # from this line and put it in front of 7, now the readout will indicate a smaller range, but the advantage is that the read values ​​are more stable.
  9. Print the read value 
  10. wait 1 second and start again

Then press save and save the program on your own computer.

Press the red stop sign button to restart the backend.
(Thonny will now reconnect to your Pico)

Then you could press the green button (Run current script).

If the pico is now removed from the computer and reconnected, the program will not be started.
We only tested the programming code.

To enable automatic starting, we must save the program again, but then select the pico.
File > Save As > Raspberry Pi Pico

Save the program with its name main.py. The pico will automatically start the program with this name when connecting the Pico to a USB port.
In this case you do need a connection with your PC and Thonny to see the measured results!

Step 4 – Result: read analog sensor with the Raspberry Pi Pico

You have now learned how to read an analog sensor yourself using a Raspberry Pi Peak.
Did you enjoy doing this project? Check out our other projects, or Les five from this Pico series!

The rating of www.elektronicavoorjou.nl at WebwinkelKeur Reviews is 9.3/10 based on 5004 reviews.