This repo is for me to keep track of the projects I build with the components in the the Pi Hut Advent Calendar and to have as a reference for myself.
I'll be following some of the projects in the guide but coming up with more challenging projects that use the components each day.
The first day focuses on setting up and getting familiar with the Pico.
Pico Info:
- RP2040 microcontroller chip
- GPIO (General Purpose Input Output) pins
- A map of Pico pins here
Software set up with Thonny.
The instructions here didn't work for me as there is a known issue with Mac. There's a blog post from Raspberry Pi going into detail on this, but their suggested fixes didn't work for me. After some reading for potential fixes, I did the following, which ended up being closer to the actual Raspberry Pi docs:
- connected Pico to Mac holding the
BOOTSEL
button on the Pico - A drive called
RP1-RP2
appears in the sidebar in finder - Navigate to the Raspberry Pi Docs for MicroPython. Or go through the Pico by:
- Open the
RP1-RP2
drive - Open the
index.htm
file (takes you here) - Open the link to the Getting help with MicroPython
- Open the
- Go to section on Drag and Drop MicroPython
- Download the correct MicroPython UF2 file for the Pico board
- Drag and drop the UF2 file from downloads to the
RP1-RP2
drive - The file copies over and the Pico reboots
- Here you'll encounter the bug, a message is displayed saying
Disk Not Ejected Properly
- ignore it - Disconnect/reconnect the USB without holding the
BOOTSEL
button - the drive should not appear in the sidebar - Open a terminal and run this command
ls -ls /dev/cu.*
- Listed you should see something like
/dev/cu.usb...
- showing the Pico is connected - Open Thonny
- In the bottom right hand corner click on
Local Python 3 - Thonny Python
- Select
MicroPython (Raspberry Pi Pico)
- you should see/dev/cu.usb...
referenced after on the same line - That should be it, try writing some code to run on the Pico!
Following on from this I did check the day 1 instructions on the Pi Hut's website under 'Setting up Thonny' and did the following when the Pico was connected:
- Open Thonny and navigate to
Run > Configure Interpreter
in the menu bar - Under
Port
, there's drop down which should have your Pico listed something likeBoard in FS Mode (/dev/cu.usb...)
- Install MicroPico extension.
- In project folder open the command pallet using
sft cmd p
. - Run
MicroPico > Configure Project
.
Day 2 - LEDs
- 330 ohm resistors
- LEDs have a specific polarity, electricity can only flow in one direction. The long leg is the Anode (+) and the short leg is the Cathode (-). Current must always flow from the anode to the cathode.
- It doesn't matter which side of the LED the resistor sits, as long as it's there limiting the flow of current for the entire circuit
- Text to Morse code translator - Enter some text and it will be shown on the LEDs in Morse code.
Day 3 - Buttons
# Using pull downs
Pin.PULL_DOWN
# Pass as an argument in Pin() function
redButton = Pin(3, Pin.IN, Pin.PULL_DOWN)
A button sends 3.3V to the GPIO pin to set it to HIGH
. It needs to be LOW
first or the code might not be able to detect a change / trigger randomly. A GPIO pin without a pull down can be 'floating' between 0V and 3.3V. Pulling down the pin to 0V ensures it always starts on LOW
.
- LED Memory Game - Randomly generate a sequence of lights and using the buttons, enter the combination back to the Pico with three difficulty levels.
Day 4 - Analogue Inputs with a Potentiometer
- Digital inputs are strictly 1 and 0 (HIGH/LOW, ON/OFF). Analogue inputs have a wide range of numbers to represent values.
- The potentiometer can pass these analogue signals in the form of voltage to the Pico.
- A potentiometer is a variable resistor.
- The potentiometer here is a 10K potentiometer, it can be set anywhere from 0 ohms to 10,000 ohms (resistance is measured in ohms).
- The right pin needs to connect to the 3V3(OUT) pin (physical pin 36), the middle pin to GPIO 27 (physical pin 32) and the left pin to GND (use the blue lane as we already have that connected to GND for the LEDs).
- An ADC is an Analogue to Digital Converter. The ADC pins on our Pico have the special ability of being able to convert an analogue input into a digital form we can use. We have to import ADC in our code to use this feature.
from machine import ADC
to use.- GPIO26, 27 and 28 (physical pins 31,32 and 34) - a map of Pico pins here.
- The Pico's ADC pins are 12-bit, however MicroPython scales this to a 16-bit range which is 0-65535.
- Invoke
.read_u16()
to scale to 16-bit.
- A type of digital signal where we, over a set period of time, decide how long the signal is ON (HIGH) and how long it's OFF (LOW).
- This can create a fading effect for LEDs but also is used for things like servos.
- Our code needs some values from us to enable it to run PWM for us -
Duty Cycle
andFrequency
. - The
duty cycle
is the percentage of the time that our LED will be ON. The higher theduty cycle
, the longer the LED will be ON, and the brighter our LED will appear. Duty cycle
for the Pico in MicroPython can range from 0 to 65535, which matches the output of the potentiometer (0-65535) so we can use this value directly without having to manipulate it.Frequency
is the number of times per second that we will repeat the ON/OFF cycle.
- Testing effects using PWN programmatically rather than with the potentiometer.
- Pulse LEDs
- Fade LEDs
Day 5 - Sound
Red into pin 17, black into the GND pin at 18.
Frequency
changes the tone (between 10 and 10000)Duty Cycle
changes the volume. The range is 0 to 65535. 10000 seems good. 0 is mute.
Day 6 - Analogue readings from a light sensor
- 1x Photo Transistor
- 1x 10k Resistor
- 3x Male to male jumper wires
Now we need to add three wires and the resistor:
- Connect the right (short) leg to the 3.3V pin (physical pin 36)
- Connect the left (long) leg to GPIO 26 (physical pin 31)
- Add the resistor to connect the left leg over to another empty unused lane on your breadboard
- Connect the other end of the resistor to a GND pin on your Pico (we're using the blue lane as it's already connected to GND for our LEDs)
Day 7 - Monitoring Motion
- 1x Mini PIR (Passive Infra-Red) Sensor - detects movement via heat.
- 3x Male to male jumper wires
Keep the same from day 5 with the below on the breakout board.
Day 8 - Temperature
- 1x Custom waterproof temperature sensor with male jumper wires
- 1x 4.7k resistor
The metal tip houses the sensor inside (specifically a DS18B20-compatible 1-wire sensor). 1-wire is a serial communication protocol for devices to communicate to a controller. 1-wire allows a special device like our temperature sensor to talk to a controller, like our Pico, enabling it to send data to the Pico to be used in a program.
Despite it's name, you actually need at least two wires to use 1-wire devices, and in some examples (like our sensor) we're using three wires. The name is related to the data communication - you only need one wire to allow the devices to talk to each other both ways.
Same as day 5 with the below on the breakout board.
Specify we're using the DS18B20
sensor using:
sensor = ds18x20.DS18X20(onewire.OneWire(SensorPin))
We also need to scan for 1-wire devices on the GPIO pin we have set up. 1-wire devices such as our sensor have a unique registration number in their read-only memory (rom). The following line in our code below scans for sensors (roms) connected to our pin:
roms = sensor.scan()
Day 9 - Tilt Switches
- 1x Tilt sensor/switch -
HIGH
when tilted andLOW
when not. - 2x Male to male jumper wires
- 2x Male to female jumper wires
Same as day 5 with the below on the breakout board.
Day 10 - Break Beam Sensor
- 1x Custom Break Beam Sensor (emitter and receiver) with male jumper wire ends
- Emitter has two wires
- Receiver has three wires
- 1x 10k resistor
- When the beam is broken (the receiver doesn't detect a signal) sends
LOW
to the pin. - When the beam is intact sends
HIGH
to the pin. - Watch for
LOW
signals.
Same as day 5 with the below on the breakout board.
Day 11 - OLED Screen
- 1x Pre-soldered 0.96" IC2 OLED I2C display (128x32)
I2C, sometimes referred to as IIC, stands for Inter-Integrated Circuit. It's another type of communication protocol which allows multiple I2C devices to communicate to a controller.
I2C requires just two wires to communicate (along with 3.3V and GND for our display) and has benefits over some other communication options.
Need to install the
micropython-ssd1306
library.
- Display Current Weather - Using the OpenWeatherMap API. Won't work with the Pico in box.
Day 12 - LED Strip
- Addressable LED Strip