This guide provides step-by-step instructions for setting up a Raspberry Pi Pico microcontroller board and writing scripts to run on it.
- Raspberry Pi Pico board
- Micro USB cable
- Computer with USB port
-
Connect Raspberry Pi Pico to Computer:
- Plug one end of the micro USB cable into the Raspberry Pi Pico's USB port.
- Connect the other end of the cable to a USB port on your computer.
-
Prepare Development Environment:
- Download and install the latest version of Thonny IDE from thonny.org.
- Launch Thonny IDE on your computer.
-
Download MicroPython Firmware:
- Go to raspberrypi.org/software and download the latest MicroPython firmware for Raspberry Pi Pico.
-
Flash MicroPython Firmware:
- Open Thonny IDE.
- Go to
Tools
>Options
>Interpreter
. - Select
MicroPython (Raspberry Pi Pico)
as the interpreter. - Click
Install or update firmware
. - Follow the prompts to flash the MicroPython firmware onto the Raspberry Pi Pico.
-
Open New Script:
- In Thonny IDE, click
File
>New
.
- In Thonny IDE, click
-
Write the Script:
from machine import Pin import time led = Pin(25, Pin.OUT) while True: led.toggle() time.sleep(0.5)
- Open New Script:
- In Thonny IDE, click
File
>New
.
- In Thonny IDE, click
- Write the Script:
import time import os import usb_hid from adafruit_hid.keycode import Keycode from adafruit_hid.keyboard import Keyboard from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS time.sleep(3) if not 'network.txt' in os.listdir(): time.sleep(1.5) keyboard = Keyboard(usb_hid.devices) layout = KeyboardLayoutUS(keyboard) keyboard.send(Keycode.WINDOWS) time.sleep(0.60) layout.write("powershell\n") time.sleep(1.0) keyboard.send(Keycode.ENTER) time.sleep(0.30) keyboard.send(Keycode._ARROW) time.sleep(0.30) keyboard.send(Keycode.ENTER) time.sleep(0.30) keyboard = Keyboard(usb_hid.devices) layout = KeyboardLayoutUS(keyboard) keyboard.send(Keycode.ENTER) time.sleep(0.17) layout.write(" powershell -Command Start-Process powershell -Verb RunAs \n") keyboard.send("enter") keyboard.send(Keycode._ARROW) time.sleep(1.5) exit() keyboard.send(Keycode.ALT, Keycode.F4)
firewall.off.1.1.mp4
This Python script demonstrates keyboard automation using a Raspberry Pi Pico microcontroller board. The script utilizes the adafruit_hid
library to emulate keypresses and interact with the host computer.
The script performs the following actions:
-
Imports:
- Imports necessary modules including
time
,os
,usb_hid
,Keycode
,Keyboard
, andKeyboardLayoutUS
from theadafruit_hid
library.
- Imports necessary modules including
-
Initial Delay:
- Waits for 3 seconds (
time.sleep(3)
) to allow time for USB enumeration after connecting the Raspberry Pi Pico to the computer.
- Waits for 3 seconds (
-
File Check:
- Checks if a file named
network.txt
does not exist in the current directory usingos.listdir()
. - If the file does not exist, it waits for an additional 1.5 seconds (
time.sleep(1.5)
).
- Checks if a file named
-
Keyboard Initialization:
- Initializes a virtual keyboard (
keyboard
) and sets the keyboard layout to US (layout = KeyboardLayoutUS(keyboard)
).
- Initializes a virtual keyboard (
-
Keyboard Actions:
- Emulates keyboard actions to interact with the host computer:
- Presses the
Windows
key (Keycode.WINDOWS
) to open the Start menu. - Types
powershell
followed by pressingEnter
to launch PowerShell (layout.write("powershell\n")
). - Navigates to the
Run as Administrator
option in the PowerShell menu by sendingARROW_RIGHT
key and thenENTER
. - Executes the command
powershell -Command Start-Process powershell -Verb RunAs
to start a new PowerShell instance with administrative privileges. - Presses
ENTER
andARROW_DOWN
keys to confirm the action and proceed.
- Presses the
- Emulates keyboard actions to interact with the host computer:
-
Script Termination:
- Exits the script (
exit()
) and sendsALT + F4
key combination to close the PowerShell window (keyboard.send(Keycode.ALT, Keycode.F4)
).
- Exits the script (
-
Setup:
- Connect the Raspberry Pi Pico to your computer using a micro USB cable.
-
Software Setup:
- Install the necessary libraries (
adafruit_hid
) for Raspberry Pi Pico development. - Use a compatible IDE like Thonny to edit and run Python scripts on the Raspberry Pi Pico.
- Install the necessary libraries (
-
Execution:
- Copy the provided Python script (
keyboard_automation.py
) onto your Raspberry Pi Pico. - Open the script in Thonny IDE and run it to execute the keyboard automation actions.
- Copy the provided Python script (
- This script demonstrates basic keyboard automation functionalities using Raspberry Pi Pico and
adafruit_hid
library. - Use caution when running scripts that emulate keyboard actions, especially those that interact with system-level functions like opening PowerShell with administrative privileges.
For more information on Raspberry Pi Pico and adafruit_hid
library, refer to the official documentation and resources available at raspberrypi.org and Adafruit Learn.