Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worksheets for Raspberry Pi Pico and micropython #7

Open
aannenko opened this issue Dec 5, 2021 · 11 comments
Open

Worksheets for Raspberry Pi Pico and micropython #7

aannenko opened this issue Dec 5, 2021 · 11 comments

Comments

@aannenko
Copy link

aannenko commented Dec 5, 2021

Hi,

I would like to suggest an idea to add worksheets for Raspberry Pi Pico to this repository.

Micropython port for Pico has everything needed to implement all projects from the worksheets. It even has a built-in support for a DS18x20 family of temperature sensors, so the code in the third worksheet could look like this:

# CamJam EduKit 2 - Sensors
# Worksheet 3 - Temperature

# Import Libraries
import time
from machine import Pin
from onewire import OneWire
from ds18x20 import DS18X20

# Initialize the temperature sensor.
ds = DS18X20(OneWire(Pin(22)))
roms = ds.scan()

# Print out the temperature until the program is stopped.
while True:
    ds.convert_temp()
    time.sleep(1)
    print(ds.read_temp(roms[0]))

or a Timer could be used to keep the Pico responsive:

# CamJam EduKit 2 - Sensors
# Worksheet 3 - Temperature

# Import Libraries
from machine import Pin, Timer
from onewire import OneWire
from ds18x20 import DS18X20

# Initialize the temperature sensor.
ds = DS18X20(OneWire(Pin(22)))
roms = ds.scan()

# A function that reads the sensor data.
def print_temp(timer):
    ds.convert_temp()
    print(ds.read_temp(roms[0]))

# Print out the temperature until the program is stopped.
Timer(period=1000, mode=Timer.PERIODIC, callback=print_temp)

Here's a setup:

pico_temperature

To implement multiple projects from the worksheets together, jump wires could be connected directly to the pins on the Pico. Although it would not look as clean as with a Raspberry Pi, it would still work.

@GeekyTim
Copy link
Member

GeekyTim commented Dec 6, 2021

We do plan to do this (and should have by now) but time has not been on our side. Thank you for the prompt and this code. It will encourage us to progress this.

@aannenko
Copy link
Author

aannenko commented Dec 6, 2021

Thank you.
If it helps, I will later post pieces of code for the other worksheets here.

@GeekyTim
Copy link
Member

GeekyTim commented Dec 6, 2021

That would be extremely useful. Thank you.

@aannenko
Copy link
Author

aannenko commented Mar 5, 2022

Worksheet Two

Circuit

All components on the breadboard (LEDs, resistors, buzzer, jump wires) are connected exactly as the Worksheet Two (GPIO Zero) describes.

The following pins of the Pico are used:

Pin 3 (GND)
Pin 4 (GP2) RED
Pin 5 (GP3) BLUE
Pin 6 (GP4) BUZZ

image

Code

# CamJam EduKit 2 - Sensors (Raspberry Pi Pico)
# Worksheet 2 - LEDs and Buzzer

# Import Python libraries
from machine import Pin
import time

# Set up the LEDs and Buzzer
red = Pin(2, Pin.OUT)
blue = Pin(3, Pin.OUT)
buzzer = Pin(4, Pin.OUT)

print("Lights and sound on")
red.on()
blue.on()
buzzer.on()

# Pause for one second
time.sleep(1)

print("Lights and sound off")
red.off()
blue.off()
buzzer.off()

@GeekyTim
Copy link
Member

GeekyTim commented Mar 6, 2022 via email

@aannenko
Copy link
Author

aannenko commented Mar 6, 2022

Worksheet Three

In contrast with my initial suggestion, LEDs and Buzzer from the Worksheet Two are still connected

Circuit

Thermal sensor is connected to the bread board in a very similar way to how the Worksheet Three (GPIO Zero) suggests. The only difference is that all connections on the rows with yellow and black wire of the sensor are shifted by two rows to the left.

The following pins of the Pico are used:

Pin 38 (GND) BLACK
Pin 36 (3v3) RED
Pin 34 (GP28) YELLOW

20220306_172457

Code

Read temperature in a cycle, pause using time.Sleep(1):

# CamJam EduKit 2 - Sensors (Raspberry Pi Pico)
# Worksheet 3 - Temperature

# Import Libraries
import time
from machine import Pin
from onewire import OneWire
from ds18x20 import DS18X20

# Initialize the temperature sensor.
ds = DS18X20(OneWire(Pin(28)))
roms = ds.scan()

# Print out the temperature until the program is stopped.
while True:
    ds.convert_temp()
    time.sleep(1)
    print(ds.read_temp(roms[0]))

Fire the print_temp function using a timer:

# CamJam EduKit 2 - Sensors (Raspberry Pi Pico)
# Worksheet 3 - Temperature

# Import Libraries
from machine import Pin, Timer
from onewire import OneWire
from ds18x20 import DS18X20

# Initialize the temperature sensor.
ds = DS18X20(OneWire(Pin(28)))
roms = ds.scan()

# A function that reads the sensor data.
def print_temp(timer):
    ds.convert_temp()
    print(ds.read_temp(roms[0]))

# Print out the temperature until the program is stopped.
Timer(period=1000, mode=Timer.PERIODIC, callback=print_temp)

@aannenko
Copy link
Author

aannenko commented Mar 12, 2022

Worksheet Four

Two different ways of reading an LDR with Pico are described below:

  • adaptation of the Worksheet Four for RPi.GPIO: complicated circuit and longer code
  • using the Pico's ADC: basic circuit without a capacitor, shorter code

Circuit (RPi.GPIO adaptation)

The LDR and the capacitor are connected to the board the same way as described in Worksheet Four (RPi.GPIO and GPIO Zero).

The following pins of the Pico are used:

Pin 36 (3v3) Positive capacitor leg connected to the Pico's 3v3 via the LDR
Pin 33 (GND) Negative capacitor leg
Pin 32 (GP27) The pin that reads the capacitor state

IMG_20220312_203204

Code (RPi.GPIO adaptation)

# CamJam EduKit 2 - Sensors (Raspberry Pi Pico)
# Worksheet 4 - Light

# Import Libraries
import time
from machine import Pin

# A variable that lets us control the LDR
ldr = Pin(27, Pin.OUT)

def readldr():
    ldrcount = 0 # Sets the count to 0
    ldr.init(Pin.OUT)
    ldr.off()
    time.sleep(0.1) # Drains all charge from the capacitor
    ldr.init(Pin.IN) # Sets the pin to be input
    # While the input pin reads 'off' or Low, count
    while (ldr.value() == 0):
        ldrcount += 1 # Add one to the counter
    return ldrcount

while True:
    print(readldr())
    time.sleep(1) # Wait for a second

# Output:
# 312 (lights on)
# 309
# 4508 (off)
# 5330 
# 1555 (dim)
# 1512

Circuit (using ADC)

The following pins of the Pico are used:

Pin 33 (GND) One LDR leg
Pin 32 (GP27, ADC) The other LDR leg

IMG_20220312_211517

Code (using ADC)

# CamJam EduKit 2 - Sensors (Raspberry Pi Pico)
# Worksheet 4 - Light

# Import Libraries
import time
from machine import ADC

# A variable that lets us control the LDR
ldr = ADC(27)

while True:
    print(ldr.read_u16())
    time.sleep(1) # Wait for a second

# Output:
# 448 (lights on)
# 448
# 1152 (off)
# 1216
# 976 (dim)
# 992

@aannenko
Copy link
Author

aannenko commented Mar 13, 2022

Worksheet Five

Circuit

The infrared sensor is connected to the bread board in a similar way to how the Worksheet Five (GPIO Zero) suggests.

The following pins of the Pico are used:

Pin 40 (VBUS) Connected to the sensor's VCC pin
Pin 38 (GND) Connected to the sensor's GND pin
Pin 34 (GP28) Connected to the sensor's OUT pin

IMG_20220313_110953

Code

# CamJam EduKit 2 - Sensors (Raspberry Pi Pico)
# Worksheet 5 - Movement

# Import Python header files
import time
from machine import Pin

print("PIR Module Test (CTRL-C to exit)")

# Set pin as input
pir = Pin(28, Pin.IN)

# Variables to hold the current and last states
currentstate = 0
previousstate = 0

try:
    print("Waiting for PIR to settle ...")
    # Loop until PIR output is 0
    while pir.value() == 1:
        currentstate = 0

    print("    Ready")
    # Loop until user quits with CTRL-C
    while True:
        # Read PIR state
        currentstate = pir.value()

        # If the PIR is triggered
        if currentstate == 1 and previousstate == 0:
            print("    Motion detected!")
        # If the PIR has returned to ready state
        elif currentstate == 0 and previousstate == 1:
            print("    Ready")            

        # Record previous state
        previousstate = currentstate
        # Wait for 10 milliseconds
        time.sleep(0.01)

except KeyboardInterrupt:
    print("    Quit")

@aannenko
Copy link
Author

aannenko commented Mar 13, 2022

Worksheet Six

Circuit

The circuits for LEDs and buzzer have not changed since Worksheet Two.
PIR is connected the same way as in the Worksheet Five.

Code

# CamJam EduKit 2 - Sensors (Raspberry Pi Pico)
# Worksheet 5 - Movement

# Import Python header files
import time
from machine import Pin

print("PIR Module Test (CTRL-C to exit)")

# Set pins as input/output
pir = Pin(28, Pin.IN)
red = Pin(2, Pin.OUT)
blue = Pin(3, Pin.OUT)
buzzer = Pin(4, Pin.OUT)

# Variables to hold the current and last states
currentstate = 0
previousstate = 0

try:
    print("Waiting for PIR to settle ...")
    # Loop until PIR output is 0
    while pir.value() == 1:
        currentstate = 0

    print("    Ready")
    # Loop until user quits with CTRL-C
    while True:
        # Read PIR state
        currentstate = pir.value()

        if currentstate == 1 and previousstate == 0:
            # PIR is triggered
            print("    Motion detected!")
            # Flash lights and sound buzzer three times
            for x in range(0, 3):
                buzzer.on()
                red.on()
                time.sleep(0.2)
                red.off()
                blue.on()
                time.sleep(0.2)
                blue.off()
                buzzer.off()
                time.sleep(0.2)

        # If the PIR has returned to ready state
        elif currentstate == 0 and previousstate == 1:
            # PIR has returned to ready state
            print("    Ready")

        previousstate = currentstate
        # Wait for 10 milliseconds
        time.sleep(0.01)

except KeyboardInterrupt:
    print("    Quit")

@GeekyTim
Copy link
Member

GeekyTim commented Oct 11, 2022 via email

@aannenko
Copy link
Author

Great! Thanks for the heads up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants