CamJam-EduKit / EduKit2

CamJam EduKit 2 - Sensors
MIT License
42 stars 27 forks source link

Worksheets for Raspberry Pi Pico and micropython #7

Open aannenko opened 2 years ago

aannenko commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

GeekyTim commented 2 years ago

That would be extremely useful. Thank you.

aannenko commented 2 years ago

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 commented 2 years ago

Thank you.

On Sat, 5 Mar 2022, 16:57 Andrii Annenko, @.***> wrote:

Worksheet Two Pinout

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: image] https://user-images.githubusercontent.com/4640265/156892226-b156ee9f-4405-4609-860f-c2c634ba28ea.png Code

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

Import Python librariesfrom machine import Pinimport utime

Set up the LEDs and Buzzerred = 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 secondutime.sleep(1)

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

— Reply to this email directly, view it on GitHub https://github.com/CamJam-EduKit/EduKit2/issues/7#issuecomment-1059796329, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDNQQXRW4YH5G26GWHS2JTU6OG6TANCNFSM5JNGN56Q . You are receiving this because you commented.Message ID: @.***>

aannenko commented 2 years ago

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 commented 2 years ago

Worksheet Four

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


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 commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

Thank you so much. I plan to work on the 'official' worksheets when I am next ashore.

On Sun, 13 Mar 2022, 11:56 Andrii Annenko, @.***> wrote:

Worksheet Six Circuit

Same as in the Worksheet Five Code

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

Import Python header filesimport timefrom machine import Pin

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

Set pins as input/outputpir = 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 statescurrentstate = 0previousstate = 0

try: print("Waiting for PIR to settle ...")

Loop until PIR output is 0

while pir.value() == 1:
    currentstate = 0

print("    Ready")
# Loop until users 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")

— Reply to this email directly, view it on GitHub https://github.com/CamJam-EduKit/EduKit2/issues/7#issuecomment-1066085503, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDNQQV5SXLZW672ZK5YS6DU7XJWHANCNFSM5JNGN56Q . You are receiving this because you commented.Message ID: @.***>

aannenko commented 1 year ago

Great! Thanks for the heads up!