├── Traffic_light_controller ├── simple_traffic_light.py └── traffic_light.py ├── burglar_alarm ├── Burglar_Alarm.py ├── Burglar_Alarm2.py ├── Burglar_Alarm3.py └── Burglar_Alarm4.py ├── file_system └── fileRW.py ├── lcd ├── HelloWorld.py ├── Hello_Pico.py ├── I2C_LCD.py ├── I2C_LCD_driver.py └── RollText.py ├── potentiometer └── potentiometer.py ├── pwm └── pwm.py ├── reaction_game ├── reaction_game.py ├── reaction_game_2.py └── reaction_game_3.py ├── temperature └── temperature.py └── ws2812b └── ws2812b.py /Traffic_light_controller/simple_traffic_light.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | led_red = machine.Pin(15, machine.Pin.OUT) 4 | led_amber = machine.Pin(14, machine.Pin.OUT) 5 | led_green = machine.Pin(13, machine.Pin.OUT) 6 | while True: 7 | led_red.value(1) 8 | utime.sleep(5) 9 | led_amber.value(1) 10 | utime.sleep(2) 11 | led_red.value(0) 12 | led_amber.value(0) 13 | led_green.value(1) 14 | utime.sleep(5) 15 | led_green.value(0) 16 | led_amber.value(1) 17 | utime.sleep(5) 18 | led_amber.value(0) 19 | -------------------------------------------------------------------------------- /Traffic_light_controller/traffic_light.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | import _thread 4 | led_red = machine.Pin(15, machine.Pin.OUT) 5 | led_amber = machine.Pin(14, machine.Pin.OUT) 6 | led_green = machine.Pin(13, machine.Pin.OUT) 7 | 8 | button = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN) 9 | buzzer = machine.Pin(12, machine.Pin.OUT) 10 | global button_pressed 11 | button_pressed = False 12 | 13 | def button_reader_thread(): 14 | global button_pressed 15 | while True: 16 | if button.value() == 1: 17 | button_pressed = True 18 | utime.sleep(0.01) 19 | _thread.start_new_thread(button_reader_thread, ()) 20 | while True: 21 | if button_pressed == True: 22 | led_red.value(1) 23 | for i in range(10): 24 | buzzer.value(1) 25 | utime.sleep(0.2) 26 | buzzer.value(0) 27 | utime.sleep(0.2) 28 | global button_pressed 29 | button_pressed = False 30 | led_red.value(1) 31 | utime.sleep(5) 32 | led_amber.value(1) 33 | utime.sleep(2) 34 | led_red.value(0) 35 | led_amber.value(0) 36 | led_green.value(1) 37 | utime.sleep(5) 38 | led_green.value(0) 39 | led_amber.value(1) 40 | utime.sleep(5) 41 | led_amber.value(0) 42 | -------------------------------------------------------------------------------- /burglar_alarm/Burglar_Alarm.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | sensor_pir = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN) 4 | def pir_handler(pin): 5 | utime.sleep_ms(100) 6 | if pin.value(): 7 | print("ALARM! Motion detected!") 8 | sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler) -------------------------------------------------------------------------------- /burglar_alarm/Burglar_Alarm2.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | sensor_pir = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN) 4 | led = machine.Pin(15, machine.Pin.OUT) 5 | def pir_handler(pin): 6 | utime.sleep_ms(100) 7 | if pin.value(): 8 | print("ALARM! Motion detected!") 9 | for i in range(50): 10 | led.toggle() 11 | utime.sleep_ms(100) 12 | sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler) 13 | while True: 14 | led.toggle() 15 | utime.sleep(5) -------------------------------------------------------------------------------- /burglar_alarm/Burglar_Alarm3.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | sensor_pir = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN) 4 | led = machine.Pin(15, machine.Pin.OUT) 5 | buzzer = machine.Pin(14, machine.Pin.OUT) 6 | def pir_handler(pin): 7 | utime.sleep_ms(100) 8 | if pin.value(): 9 | print("ALARM! Motion detected!") 10 | for i in range(50): 11 | led.toggle() 12 | buzzer.toggle() 13 | utime.sleep_ms(100) 14 | sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler) 15 | while True: 16 | led.toggle() 17 | utime.sleep(5) -------------------------------------------------------------------------------- /burglar_alarm/Burglar_Alarm4.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | sensor_pir = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN) 4 | sensor_pir2 = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_DOWN) 5 | led = machine.Pin(15, machine.Pin.OUT) 6 | buzzer = machine.Pin(14, machine.Pin.OUT) 7 | def pir_handler(pin): 8 | utime.sleep_ms(100) 9 | if pin.value(): 10 | if pin is sensor_pir: 11 | print("ALARM! Motion detected in bedroom!") 12 | elif pin is sensor_pir2: 13 | print("ALARM! Motion detected in living room!") 14 | for i in range(50): 15 | led.toggle() 16 | buzzer.toggle() 17 | utime.sleep_ms(100) 18 | sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler) 19 | sensor_pir2.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler) 20 | while True: 21 | led.toggle() 22 | utime.sleep(5) -------------------------------------------------------------------------------- /file_system/fileRW.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | sensor_temp = machine.ADC(machine.ADC.CORE_TEMP) 4 | conversion_factor = 3.3 / (65535) 5 | reading = sensor_temp.read_u16() * conversion_factor 6 | temperature = 27 - (reading - 0.706)/0.001721 7 | file = open("temps.txt","w") 8 | file.write(str(temperature)) 9 | file.read() 10 | file.close() 11 | file = open("temps.txt") 12 | print(file.read()) 13 | file.close() -------------------------------------------------------------------------------- /lcd/HelloWorld.py: -------------------------------------------------------------------------------- 1 | import I2C_LCD_driver 2 | from time import * 3 | 4 | mylcd = I2C_LCD_driver.lcd() 5 | 6 | mylcd.lcd_display_string("Hello World!", 1) -------------------------------------------------------------------------------- /lcd/Hello_Pico.py: -------------------------------------------------------------------------------- 1 | import I2C_LCD_driver 2 | from time import * 3 | 4 | mylcd = I2C_LCD_driver.lcd() 5 | 6 | mylcd.lcd_display_string("Hello Pico!", 1) 7 | -------------------------------------------------------------------------------- /lcd/I2C_LCD.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C 2 | 3 | i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=100000) 4 | print(i2c.scan()) 5 | -------------------------------------------------------------------------------- /lcd/I2C_LCD_driver.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Original code found at: 3 | # https://gist.github.com/DenisFromHR/cc863375a6e19dce359d 4 | 5 | """ 6 | Compiled, mashed and generally mutilated 2014-2015 by Denis Pleic 7 | Made available under GNU GENERAL PUBLIC LICENSE 8 | 9 | # Modified Python I2C library for Raspberry Pi 10 | # as found on http://www.recantha.co.uk/blog/?p=4849 11 | # Joined existing 'i2c_lib.py' and 'lcddriver.py' into a single library 12 | # added bits and pieces from various sources 13 | # By DenisFromHR (Denis Pleic) 14 | # 2015-02-10, ver 0.1 15 | 16 | """ 17 | 18 | # i2c bus (0 -- original Pi, 1 -- Rev 2 Pi) 19 | I2CBUS = 0 20 | 21 | # LCD Address 22 | ADDRESS = 0x27 23 | 24 | from machine import Pin, I2C 25 | from time import sleep 26 | 27 | i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=100000) 28 | 29 | class i2c_device: 30 | def __init__(self, addr, port=I2CBUS): 31 | self.addr = addr 32 | self.bus = I2C(port, scl=Pin(1), sda=Pin(0), freq=400000) 33 | 34 | # Write a single command 35 | def write_cmd(self, cmd): 36 | self.bus.writeto(self.addr, bytes([cmd])) 37 | sleep(0.0001) 38 | 39 | 40 | # commands 41 | LCD_CLEARDISPLAY = 0x01 42 | LCD_RETURNHOME = 0x02 43 | LCD_ENTRYMODESET = 0x04 44 | LCD_DISPLAYCONTROL = 0x08 45 | LCD_CURSORSHIFT = 0x10 46 | LCD_FUNCTIONSET = 0x20 47 | LCD_SETCGRAMADDR = 0x40 48 | LCD_SETDDRAMADDR = 0x80 49 | 50 | # flags for display entry mode 51 | LCD_ENTRYRIGHT = 0x00 52 | LCD_ENTRYLEFT = 0x02 53 | LCD_ENTRYSHIFTINCREMENT = 0x01 54 | LCD_ENTRYSHIFTDECREMENT = 0x00 55 | 56 | # flags for display on/off control 57 | LCD_DISPLAYON = 0x04 58 | LCD_DISPLAYOFF = 0x00 59 | LCD_CURSORON = 0x02 60 | LCD_CURSOROFF = 0x00 61 | LCD_BLINKON = 0x01 62 | LCD_BLINKOFF = 0x00 63 | 64 | # flags for display/cursor shift 65 | LCD_DISPLAYMOVE = 0x08 66 | LCD_CURSORMOVE = 0x00 67 | LCD_MOVERIGHT = 0x04 68 | LCD_MOVELEFT = 0x00 69 | 70 | # flags for function set 71 | LCD_8BITMODE = 0x10 72 | LCD_4BITMODE = 0x00 73 | LCD_2LINE = 0x08 74 | LCD_1LINE = 0x00 75 | LCD_5x10DOTS = 0x04 76 | LCD_5x8DOTS = 0x00 77 | 78 | # flags for backlight control 79 | LCD_BACKLIGHT = 0x08 80 | LCD_NOBACKLIGHT = 0x00 81 | 82 | En = 0b00000100 # Enable bit 83 | Rw = 0b00000010 # Read/Write bit 84 | Rs = 0b00000001 # Register select bit 85 | 86 | class lcd: 87 | #initializes objects and lcd 88 | def __init__(self): 89 | self.lcd_device = i2c_device(ADDRESS) 90 | 91 | self.lcd_write(0x03) 92 | self.lcd_write(0x03) 93 | self.lcd_write(0x03) 94 | self.lcd_write(0x02) 95 | 96 | self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) 97 | self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) 98 | self.lcd_write(LCD_CLEARDISPLAY) 99 | self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) 100 | sleep(0.2) 101 | 102 | 103 | # clocks EN to latch command 104 | def lcd_strobe(self, data): 105 | self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT) 106 | sleep(.0005) 107 | self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT)) 108 | sleep(.0001) 109 | 110 | def lcd_write_four_bits(self, data): 111 | self.lcd_device.write_cmd(data | LCD_BACKLIGHT) 112 | self.lcd_strobe(data) 113 | 114 | # write a command to lcd 115 | def lcd_write(self, cmd, mode=0): 116 | self.lcd_write_four_bits(mode | (cmd & 0xF0)) 117 | self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0)) 118 | 119 | # write a character to lcd (or character rom) 0x09: backlight | RS=DR< 120 | # works! 121 | def lcd_write_char(self, charvalue, mode=1): 122 | self.lcd_write_four_bits(mode | (charvalue & 0xF0)) 123 | self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0)) 124 | 125 | # put string function with optional char positioning 126 | def lcd_display_string(self, string, line=1, pos=0): 127 | if line == 1: 128 | pos_new = pos 129 | elif line == 2: 130 | pos_new = 0x40 + pos 131 | elif line == 3: 132 | pos_new = 0x14 + pos 133 | elif line == 4: 134 | pos_new = 0x54 + pos 135 | 136 | self.lcd_write(0x80 + pos_new) 137 | 138 | for char in string: 139 | self.lcd_write(ord(char), Rs) 140 | 141 | # clear lcd and set to home 142 | def lcd_clear(self): 143 | self.lcd_write(LCD_CLEARDISPLAY) 144 | self.lcd_write(LCD_RETURNHOME) 145 | 146 | # define backlight on/off (lcd.backlight(1); off= lcd.backlight(0) 147 | def backlight(self, state): # for state, 1 = on, 0 = off 148 | if state == 1: 149 | self.lcd_device.write_cmd(LCD_BACKLIGHT) 150 | elif state == 0: 151 | self.lcd_device.write_cmd(LCD_NOBACKLIGHT) 152 | 153 | # add custom characters (0 - 7) 154 | def lcd_load_custom_chars(self, fontdata): 155 | self.lcd_write(0x40); 156 | for char in fontdata: 157 | for line in char: 158 | self.lcd_write_char(line) -------------------------------------------------------------------------------- /lcd/RollText.py: -------------------------------------------------------------------------------- 1 | import I2C_LCD_driver 2 | from time import * 3 | 4 | mylcd = I2C_LCD_driver.lcd() 5 | 6 | str_pad = " " * 16 7 | my_long_string = "This is a string that needs to scroll" 8 | my_long_string = str_pad + my_long_string 9 | 10 | while True: 11 | for i in range (0, len(my_long_string)): 12 | lcd_text = my_long_string[i:(i+16)] 13 | mylcd.lcd_display_string(lcd_text,1) 14 | sleep(0.4) 15 | mylcd.lcd_display_string(str_pad,1) -------------------------------------------------------------------------------- /potentiometer/potentiometer.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | potentiometer = machine.ADC(26) 4 | conversion_factor = 3.3 / (65535) 5 | while True: 6 | voltage = potentiometer.read_u16() * conversion_factor 7 | print(voltage) 8 | utime.sleep(2) -------------------------------------------------------------------------------- /pwm/pwm.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | potentiometer = machine.ADC(26) 4 | led = machine.PWM(machine.Pin(15)) 5 | led.freq(1000) 6 | while True: 7 | led.duty_u16(potentiometer.read_u16()) -------------------------------------------------------------------------------- /reaction_game/reaction_game.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | import urandom 4 | pressed = False 5 | led = machine.Pin(15, machine.Pin.OUT) 6 | button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN) 7 | def button_handler(pin): 8 | global pressed 9 | if not pressed: 10 | pressed=True 11 | print(pin) 12 | led.value(1) 13 | utime.sleep(urandom.uniform(5, 10)) 14 | led.value(0) 15 | button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler) -------------------------------------------------------------------------------- /reaction_game/reaction_game_2.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | import urandom 4 | pressed = False 5 | led = machine.Pin(15, machine.Pin.OUT) 6 | button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN) 7 | def button_handler(pin): 8 | global pressed 9 | if not pressed: 10 | pressed=True 11 | timer_recation = utime.ticks_diff(utime.ticks_ms(), timer_start) 12 | print("Your recation time was" + str(timer_recation) + "milliseconds!") 13 | print(pin) 14 | led.value(1) 15 | utime.sleep(urandom.uniform(5, 10)) 16 | led.value(0) 17 | timer_start = utime.ticks_ms() 18 | button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler) -------------------------------------------------------------------------------- /reaction_game/reaction_game_3.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | import urandom 4 | pressed = False 5 | led = machine.Pin(15, machine.Pin.OUT) 6 | left_button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN) 7 | right_button = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN) 8 | fastest_button = None 9 | def button_handler(pin): 10 | global pressed 11 | if not pressed: 12 | pressed=True 13 | global fastest_button 14 | fastest_button = pin 15 | led.value(1) 16 | utime.sleep(urandom.uniform(5, 10)) 17 | led.value(0) 18 | timer_start = utime.ticks_ms() 19 | left_button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler) 20 | right_button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler) 21 | while fastest_button is None: 22 | utime.sleep(1) 23 | if fastest_button is left_button: 24 | print("Left Player wins!") 25 | elif fastest_button is right_button: 26 | print("Right Player wins!") -------------------------------------------------------------------------------- /temperature/temperature.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import utime 3 | sensor_temp = machine.ADC(4) 4 | conversion_factor = 3.3 / (65535) 5 | while True: 6 | reading = sensor_temp.read_u16() * conversion_factor 7 | temperature = 27 - (reading - 0.706)/0.001721 8 | print(temperature) 9 | utime.sleep(2) -------------------------------------------------------------------------------- /ws2812b/ws2812b.py: -------------------------------------------------------------------------------- 1 | import array, utime 2 | from machine import Pin 3 | import rp2 4 | from rp2 import PIO, StateMachine, asm_pio 5 | # Configure the number of WS2812 LEDs. 6 | NUM_LEDS = 10 7 | @asm_pio(sideset_init=PIO.OUT_LOW, out_shiftdir=PIO.SHIFT_LEFT, 8 | autopull=True, pull_thresh=24) 9 | def ws2812(): 10 | T1 = 2 11 | T2 = 5 12 | T3 = 3 13 | label("bitloop") 14 | out(x, 1) .side(0) [T3 - 1] 15 | jmp(not_x, "do_zero") .side(1) [T1 - 1] 16 | jmp("bitloop") .side(1) [T2 - 1] 17 | label("do_zero") 18 | nop() .side(0) [T2 - 1] 19 | # Create the StateMachine with the ws2812 program, outputting on Pin(0). 20 | sm = StateMachine(0, ws2812, freq=8000000, sideset_base=Pin(0)) 21 | # Start the StateMachine, it will wait for data on its FIFO. 22 | sm.active(1) 23 | # Display a pattern on the LEDs via an array of LED RGB values. 24 | ar = array.array("I", [0 for _ in range(NUM_LEDS)]) 25 | print("blue") 26 | for j in range(0, 255): 27 | for i in range(NUM_LEDS): 28 | ar[i] = j 29 | sm.put(ar,8) 30 | utime.sleep_ms(10) 31 | print("red") 32 | for j in range(0, 255): 33 | for i in range(NUM_LEDS): 34 | ar[i] = j<<8 35 | sm.put(ar,8) 36 | utime.sleep_ms(10) 37 | print("green") 38 | for j in range(0, 255): 39 | for i in range(NUM_LEDS): 40 | ar[i] = j<<16 41 | sm.put(ar,8) 42 | utime.sleep_ms(10) 43 | print("white") 44 | for j in range(0, 255): 45 | for i in range(NUM_LEDS): 46 | ar[i] = (j<<16) + (j<<8) + j 47 | sm.put(ar,8) 48 | utime.sleep_ms(10) --------------------------------------------------------------------------------