Raspberry Pi Pico project: Automatic screen saver
With this project you place the distance sensor aimed at yourself when you are sitting at your PC or laptop.
Nothing will happen now, but if you get up and walk away from your PC, the screen will automatically lock.
This RPi Pico project is perfect to get started with the Thonny programming tool and CircuitPython.
You will learn to connect the Pico to your PC and install programs on it.
This manual covers:
- Level - Beginner 25%
- Duration 20 min 25%
- Costs €10,95 11%
A Raspberry Pi Pico is a microcontrollerboard which was developed by the same organization responsible for the popular Raspberry Pi single-board computers.
Some features of the Raspberry Pi Pico are:
- RP2040 microcontroller: The Pico is powered by the RP2040 microcontroller, which is specially designed by Raspberry Pi. This microcontroller has a dual-core ARM Cortex-M0+ processor and offers significant computing power for a microcontroller.
- I/O pins: The Pico has a large number of GPIO (General Purpose Input/Output) pins, with which you can connect and operate sensors, displays, motors and other electronic components.
- USB connection: The Pico has a USB port for programming and powering the board. It can be programmed with MicroPython, C/C++, and other programming languages.
- Versatility: Thanks to the powerful RP2040 and the many I/O pins, the Pico is suitable for a wide range of projects, from simple flashing LEDs to complex IoT applications.
- Low costs: From Raspberry Pi Pico is very affordable and therefore accessible to hobbyists, makers and professionals.
An HC-SR04 is an ultrasonic distance sensor commonly used in electronic projects, especially in the world of hobby electronics and robotics. This sensor uses ultrasonic sound waves to measure the distance between the sensor and an object. Here's how it works:
- Ultrasonic transmitter: The sensor contains an ultrasonic transmitter that emits sound waves at a frequency inaudible to humans (usually around 40 kHz). (Trigger)
- Recipient: The sensor also has an ultrasonic receiver to capture the reflected sound waves. (Echo)
- Time measurement: The HC-SR04 measures the time it takes for the emitted sound wave to reach the object and return to the sensor.
- Distance calculation: The measured time allows the sensor to calculate the distance to the object using the speed of sound in the air. This is usually done with the formula: Distance = (Time x Speed of Sound) / 2.
HC-SR04 sensors are easy to use and can be connected to microcontrollers such as Arduino or Raspberry Pi to measure object distances and perform obstacle detection. They are popular for projects such as building automatic door openers, robot navigation, distance measuring devices and more.
Step 2: Install software
In this Raspberry Pico we will first install CircuitPython.
Download the uf2 file from the website below:
https://circuitpython.org/board/raspberry_pi_pico/
- Press the white one bootsel 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 adafruit-circuitpython-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 under > Options > Interpreter CircuitPython (Generic)
Now some Python modules still need to be installed.
Open the Tools > Manage Packages menu.
Now search for adafruit-circuitpython-hid and adafruit-circuitpython-hcsr04 and install them
Step 3: Programming
Now you can type in the program below in the Thonny interface.
import usb_hid from adafruit_hid.keyboard importKeyboard from adafruit_hid.keycode import Keycode import board import adafruit_hcsr04 import time ultrasonic = adafruit_hcsr04.HCSR04(trigger_pin=board.GP4, echo_pin=board.GP5) toggle = 0 time.sleep(5) while True: # print((ultrasonic.distance,)) if ultrasonic.distance > 100 and toggle == 0: toggle = 1 kbd = Keyboard(usb_hid.devices) kbd.press(Keycode.LEFT_GUI) time.sleep(1) kbd.press(Keycode.L) time.sleep(0.1) kbd.release(Keycode.L) time.sleep(0.1) kbd.release (Keycode.LEFT_GUI) time.sleep(1) kdb.release_all() time.sleep(10) if ultrasonic.distance < 100: toggle = 0 time.sleep(1)
Explanation of the program:
- import the python module for keyboard/mouse emulation
- import keyboard queues
- import keycode for the keyboard (key codes)
- import the module to address the IO pins of the Pico
- import the module for reading the HC-SR04 module
- import time module which provides functions for delays.
- .
- Define an ultrasonic variable that the ultrasonic module can read. The Trigger and Echo pins are also defined here.
- A defined variable to keep track of when lock-screen is activated
- First wait 5 seconds before the meeting starts
- An infinite loop, which continues to repeat program lines 12-28
- A debug line, this prints the measured distance
- Here it is checked whether the measured distance is more than 100 cm and whether the toggle variable has not already been set.
- Apparently the toggle was not on, so that is allowed in this case
- Define the keyboard as kdb
- Send the Keycode for window-key
- Wait a moment before sending the L key
- Send the L key as a keycode
- Wait a moment before the L can be released
- Release the L key
- Wait another 10th second
- Release the windows key
- Another break
- Send a release all keys command
- Wait 10 seconds, we don't want another lock command too quickly
- If the measured distance is less than 100 cm,
- toggle can be reset to 0
- Wait 1 second before the programming loop starts again at 11
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
Sit behind your PC or laptop and point the sensor at yourself.
Nothing will happen now, but if you get up and walk away from your PC, the screen will automatically lock.