Hello there, friends! In this article, we'll look about how to utilize the Raspberry Pi Pico with the Micropython programming language. Let's see how we can code Pico together with a few simple projects.
Do not forget to visit Robotistan Maker Blog to see detailed contents related with Raspberry Pi Pico & more.
Step 1: The Required Pico KitIn the projects we'll explain, we'll utilize a Raspberry Pi Pico Mega Kit developed by Robotistan. Pico Mega Kit, which includes 34 distinct components, was designed to help you get started on projects quickly.
The kit includes a variety of important components, ranging from smart sensors to IoT modules.
You can find the Raspberry Pi Mega Kit on the Amazon.
Step 2: Button controlled LEDOn your Pico's pin diagram, the "GND" pin is the ground pin, while the "3V3 (OUT)" pin is the 36th pin. These two pins will show up in a lot of your projects. To begin, build the circuit on the breadboard as indicated in the diagram.
Then, in your Thonny IDE, modify the "blink" code you produced in the previous project. To open the "blink.py" file, double-click it on the left side of Thonny IDE. The codes you typed in the previous project will be visible. You'll now completely erase these codes and replace them with new ones.
When you looked at the code below, you defined the hardware of Pi-co using the code "from machine import Pin" as in the previous project and added the "time" library.
Using the code "led = Pin (15, Pin.OUT), " you selected the 15th numbered pin as the pin you linked to the LED. The code "button = Pin (14, Pin.IN, Pin.PULL DOWN)" indicates that the button is connected to pin 14 as the input pin.
Following that, you created a "while loop." Based on the value read from the button, it will change the state of the led in this cycle.
When you press and hold the button with the code “time.sleep (0.5)“, the LED will flash and flash at half-second intervals.
from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
led.toggle()
time.sleep(0.5)
After writing the codes, press the “Run” icon at the top and save your codes to your Pico and run them.
By pressing the button, you will be able to switch the LED on and off. Now it's time to move on to the next endeavor.
Step 3: Finding Temperature Value with PicoThe Raspberry Pi Pico includes an inbuilt temperature sensor, as I explained at the beginning of the article. Let us next learn how to utilize this temperature sensor together.
To begin, open the Thonny IDE and create a new file with the code below.
#We provided access to hardware on Raspberry Pi Pico
import machine
import utime
#We have defined the pin to which the temperature sensor is connected
sensor_temp = machine.ADC(4)
#Since we are making analog readings, we have converted the 16-bit data from the sensor into voltage data that may be meaningful to us with the "conversion factor" code. Since Pico's pin outputs 3.3 volts, we divided the 3.3 volts into 2^16 – 1 = 65535.
conversion_factor = 3.3 / (65535)
while True:
#Temperature reading
reading = sensor_temp.read_u16() * conversion_factor
temp = 27 - (reading - 0.706)/0.001721
print(temp)
utime.sleep(2)
Now let’s save these codes in our Pico and run them. When the codes start to run, we can see the temperature data from our Pico every 2 seconds in the Shell window at the bottom.
Now let’s open a new file in Thonny IDE and write the following codes.
import machine
import utime
#We have defined the pin to which the PIR sensor is connected.
sensor_pir = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
#We have defined the pin where the LED is defined
led = machine.Pin(15, machine.Pin.OUT)
#We have defined the pin where the buzzer is defined
buzzer = machine.Pin(14, machine.Pin.OUT)
def pir_handler(pin):
utime.sleep_ms(100)
if pin.value():
print("ATTENTION! Motion Detected!")
for i in range(50):
led.toggle()
buzzer.toggle()
utime.sleep_ms(100)
sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
while True:
led.toggle()
utime.sleep(5)
Now let’s save these codes in our Pico and run them. Now, when there is any situation that triggers our motion sensor, the buzzer will sound and our LED will light up. In the Shell window at the bottom, when motion is detected, “ATTENTION! Motion Detected!” text will appear.
You will create a weather monitor with a 2X16 LCD screen in this project. In this project, you'll learn how to install a library and record the temperature data you collect in a text file automatically.First, build your own circuit on the breadboard according to the circuit below.
Now download the necessary library to use our LCD screen from https://github.com/T-622/RPI-PICO-I2C-LCD. When opening the link, click the green button that says “Code” and then click the “Download ZIP” button to download the library.
Open the downloaded “*.zip” file and extract the “lcd_api.py“, “pico_i2c_lcd.py“, “pico_i2c_lcd_test.py” files to the desktop.
Open them in Thonny IDE by double clicking on these files that you extract to the desktop.
Now save these files to the “lib” folder in your Pico in order, click on the “Files” tab on the top and then click on “Save as …“.
It asks where you want to save the library file. You will choose the Raspberry Pi Pico.
In this part, you have to choose the location where you want to save the file, then type the file name first. After that double click on the “lib” folder.
Then click the “OK” button to save it to the folder
Do the same for the other two files.
Now that you have added libraries, you can open a new file by clicking the “Plus” icon on the top of a new Thonny IDE and write the following codes. When you examine the codes, first add the libraries as in other projects, then make the settings of our 2X16 LCD screen and create a new text document named “save” with the code “file = open (” record.txt “, ” w “)“.
import utime
import machine
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
dosya = open("kayit.txt", "w")
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
while True:
lcd.clear()
reading = sensor_temp.read_u16() * conversion_factor
temp = 27 - (reading - 0.706)/0.001721
lcd.putstr("Temp: ")
lcd.move_to(0,1)
lcd.putstr(str(temp))
dosya.write(str(temp) + "\n")
dosya.flush()
utime.sleep(2)
Now save these codes to your Pico and run them, then press the “Run” icon at the top.
It asks where you want to save the code. For the code to always run, you need to click on the “Raspberry Pi Pico” option and save it to our Pico.
Type “havadurumu.py” as the file name and click the “OK” button. Now, you will be able to see the temperature data on your LCD screen and at the same time this data will be recorded in a text document.
Now wait for some data to collect, then click the “Stop” icon at the top to stop the code from running.
Write the following codes in the Shell section at the bottom so that you can see the data have been saved.
file = open("kayit.txt")
print(file.read())
file.close()
After typing the “print (file.read ())” code, you can see the saved data as in the photo below.
In this project, you will measure distance with HC-SR04 distance sensor. First, create your circuit on the breadboard according to the diagram below.
Now, in Thonny IDE, create a new file and write your code. When looking at the codes, you'll notice that the first two lines define the libraries, as well as the pins to which the distance sensor is linked. The function "def echoTime ():" was also used to write the distance measuring function. You've set the distance measuring function to run every 5 seconds, as stated by the while loop.
from machine import Pin
import utime
trigger = Pin(18, Pin.OUT)
echo = Pin(21, Pin.IN)
def echoTime():
trigger.value(0)
utime.sleep_us(5)
trigger.value(1)
utime.sleep_us(10)
trigger.value(0)
try:
echoTime = machine.time_pulse_us(echo, 1, 1000000)
cm = (echoTime * 0.034321) * 0.5
print(str(cm)+" cm")
except OSError as e:
print(e)
while True:
echoTime()
utime.sleep(5)
Now save these codes to your Pico and run them, then press the “Run” icon at the top.
It asks where you want to save the code. For the code to always run, you need to click on the “Raspberry Pi Pico” option and save it to our Pico.
Write “distance.py” as the file name and click the “OK” button.
You can now see the distance in the Shell section of the Thonny IDE.
It was a pleasure to explain these steps friends. See you next time.
Comments