Delivery throughout Europe

Ordered before 16:00 = Shipped today

Fast delivery with DHL

XNUMX days return *


Country

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% 25%
  • Duration 20 min 25% 25%
  • Costs €10,95 11% 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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:

  1. Ultrasonic transmitter: The sensor contains an ultrasonic transmitter that emits sound waves at a frequency inaudible to humans (usually around 40 kHz). (Trigger)
  2. Recipient: The sensor also has an ultrasonic receiver to capture the reflected sound waves. (Echo)
  3. Time measurement: The HC-SR04 measures the time it takes for the emitted sound wave to reach the object and return to the sensor.
  4. 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

 

 

Connect the sensor to the Raspberry Pico like the diagram below.

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:

  1. import the python module for keyboard/mouse emulation
  2. import keyboard queues
  3. import keycode for the keyboard (key codes)
  4. import the module to address the IO pins of the Pico
  5. import the module for reading the HC-SR04 module
  6. import time module which provides functions for delays.
  7. .
  8. Define an ultrasonic variable that the ultrasonic module can read. The Trigger and Echo pins are also defined here.
  9. A defined variable to keep track of when lock-screen is activated
  10. First wait 5 seconds before the meeting starts
  11. An infinite loop, which continues to repeat program lines 12-28
  12. A debug line, this prints the measured distance
  13. Here it is checked whether the measured distance is more than 100 cm and whether the toggle variable has not already been set.
  14. Apparently the toggle was not on, so that is allowed in this case
  15. Define the keyboard as kdb 
  16. Send the Keycode for window-key
  17. Wait a moment before sending the L key
  18. Send the L key as a keycode
  19. Wait a moment before the L can be released
  20. Release the L key
  21. Wait another 10th second
  22. Release the windows key
  23. Another break
  24. Send a release all keys command
  25. Wait 10 seconds, we don't want another lock command too quickly
  26. If the measured distance is less than 100 cm,
  27. toggle can be reset to 0
  28. 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.

 

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