Sunday, 29 March 2026

Clock and temp using TM1637

import machine import dht import time import tm1637 # SETUP HARDWARE # Display connected to GP26 (CLK) and GP27 (DIO) display = tm1637.TM1637(clk=machine.Pin(26), dio=machine.Pin(27)) # Temperature sensor connected to GP18 sensor = dht.DHT11(machine.Pin(18)) # Set display brightness display.brightness(3) print("Clock started (using synced laptop time)...") # MAIN LOOP while True: # Get current time. # Returns a tuple: (year, month, mday, hour, minute, second, weekday, yearday) t = time.localtime() # Extract Hour (index 3) and Minute (index 4) hh = t[3] mm = t[4] # Show the time for 10 seconds display.numbers(hh, mm, True) # Colon ON time.sleep(10) # Measure and show temperature for 5 seconds try: sensor.measure() temp = sensor.temperature() display.temperature(temp) # Format example: "25 C" time.sleep(5) except: print("Sensor error") time.sleep(1)

Weather Station ThingSpeak

import network import time import dht import urequests from machine import Pin import secret # --- Configuration --- ssid=secret.WIFI_SSID password=secret.WIFI_PASS api_key = secret.THINGSPEAK_API_KEY DHT_PIN = 18 # --- LED Setup --- led_pwr = Pin(15, Pin.OUT) # Red led_wifi = Pin(14, Pin.OUT) # Green led_data = Pin(13, Pin.OUT) # Blue led_pwr.value(1) # --- Setup Sensor --- sensor = dht.DHT11(Pin(DHT_PIN)) # --- WiFi --- wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) while not wlan.isconnected(): led_wifi.toggle() time.sleep(0.5) led_wifi.value(1) # --- Main Loop --- while True: sensor.measure() t = sensor.temperature() h = sensor.humidity() # Print values to Shell print('Temp ', t) print('Humidity ', h) url = f"https://api.thingspeak.com/update?api_key={api_key}&field1={t}&field2={h}" led_data.value(1) response = urequests.get(url) response.close() led_data.value(0) time.sleep(20)

Weather station using ThingSpeak abd SSD1306

import network import time import dht import urequests from machine import Pin,I2C import ssd1306 import secret # --- Configuration --- ssid=secret.WIFI_SSID password=secret.WIFI_PASS api_key = secret.THINGSPEAK_API_KEY DHT_PIN = 18 # --- SSD1306 setup --- # Screen dimensions WIDTH = 128 HEIGHT = 64 # Initialize OLED i2c = I2C(0, scl=Pin(17), sda=Pin(16)) # GP17=SCL, GP16=SDA oled = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c) # Clear the screen oled.fill(0) # --- LED Setup --- led_pwr = Pin(15, Pin.OUT) # Red led_wifi = Pin(14, Pin.OUT) # yellow led_data = Pin(13, Pin.OUT) # Green led_pwr.value(1) # --- Setup Sensor --- sensor = dht.DHT11(Pin(DHT_PIN)) # --- WiFi --- wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) while not wlan.isconnected(): led_wifi.toggle() time.sleep(0.5) led_wifi.value(1) print('Connected to ' , wlan.ifconfig()[0]) # --- Main Loop --- while True: sensor.measure() t = sensor.temperature() h = sensor.humidity() # Print values to Shell print('Temperature ', t, '°C') print('Humidity ', h, '%') # Write to the display # Syntax: text(string, x, y) oled.text("Temp: {} degC".format(t), 0, 0) oled.text("Humidity: {} %".format(h), 0, 35) # Draw temperature bar (max width = 100 pixels) bar_width = int((t / 100) * 100) oled.rect(0, 15, 100, 10, 1) # outline oled.fill_rect(0, 15, bar_width, 10, 1) # filled bar # Draw humidity bar (max width = 100 pixels) bar_width = int((h / 100) * 100) oled.rect(0, 50, 100, 10, 1) # outline oled.fill_rect(0, 50, bar_width, 10, 1) # filled bar # Show on OLED oled.show() url = f"https://api.thingspeak.com/update?api_key={api_key}&field1={t}&field2={h}" led_data.value(1) response = urequests.get(url) response.close() led_data.value(0) time.sleep(20) # Clear the screen oled.fill(0)

Secret credentials

ssid="xxxxx" password="xxxxx" WIFI_SSID = "xxxxx" WIFI_PASS = "xxxxx" Mobile_Hotspot_SSID = "xxxxx" Mobile_Hotspot_pass = "xxxxx" THINGSPEAK_API_KEY = "xxxxx" To call these passwords, insert a line: import secret in the main program

Saturday, 3 June 2023

Motor direction 2

from machine import Pin import utime in1=Pin(15,Pin.OUT)#purple in2=Pin(14,Pin.OUT)#blue in3=Pin(17,Pin.OUT)#white in4=Pin(16,Pin.OUT)#green while True: print('Not turning') in1.value(0) in2.value(0) in3.value(0) in4.value(0) utime.sleep(5) #wheel spin towards motor controller L298N print('forward direction') in1.value(0) in2.value(1) in3.value(0) in4.value(1) utime.sleep(5) print('Not turning') in1.value(1) in2.value(1) in3.value(1) in4.value(1) utime.sleep(5) #wheel spin away from motor controller L298N print('backward direction') in1.value(1) in2.value(0) in3.value(1) in4.value(0) utime.sleep(5) #https://www.youtube.com/watch?v=dRXJAFPhKTY&list=PLgFwxqENEmX_8kdXEFYdETEqB4PPkMyc0&index=11

Motor direction 1

from machine import Pin import utime in1=Pin(14,Pin.OUT)#blue in2=Pin(15,Pin.OUT)#purple in3=Pin(16,Pin.OUT)#green in4=Pin(17,Pin.OUT)#white while True: print('Not turning') in1.value(0) in2.value(0) in3.value(0) in4.value(0) utime.sleep(5) #wheel spin away from motor controller L298N print('backward direction') in1.value(0) in2.value(1) in3.value(0) in4.value(1) utime.sleep(5) print('Not turning') in1.value(1) in2.value(1) in3.value(1) in4.value(1) utime.sleep(5) #wheel spin towards motor controller L298N print('forward direction') in1.value(1) in2.value(0) in3.value(1) in4.value(0) utime.sleep(5) #https://www.youtube.com/watch?v=dRXJAFPhKTY&list=PLgFwxqENEmX_8kdXEFYdETEqB4PPkMyc0&index=11

Wednesday, 26 April 2023

CoreElectronics 2: Create a Simple HTTP Server

Part 1 # Hardware Test # Blink and LED (GP15) slowly/quickly while a button (GP16) is not-pressed/pressed from machine import Pin from time import sleep_ms led = Pin(15, Pin.OUT) button = Pin(16, Pin.IN, Pin.PULL_UP) while True: if button.value() == 0: # button pressed delay = 100 # short delay else: delay = 1000 # long delay led.toggle() sleep_ms(delay) #https://core-electronics.com.au/guides/raspberry-pi-pico-w-create-a-simple-http-server/ #https://www.youtube.com/watch?v=AK8UYh7pMGM&t=515s Part 2 # Simple HTTP Server Example # Control an LED and read a Button using a web browser import time import network import socket from machine import Pin led = Pin(15, Pin.OUT) ledState = 'LED State Unknown' button = Pin(16, Pin.IN, Pin.PULL_UP) ssid = 'xxxxx' password = 'xxxxx' wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) html = """ Pico W

Pico W HTTP Server

Hello, World!

%s

""" # Wait for connect or fail max_wait = 10 while max_wait > 0: if wlan.status() < 0 or wlan.status() >= 3: break max_wait -= 1 print('waiting for connection...') time.sleep(1) # Handle connection error if wlan.status() != 3: raise RuntimeError('network connection failed') else: print('Connected') status = wlan.ifconfig() print( 'ip = ' + status[0] ) # Open socket addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(1) print('listening on', addr) # Listen for connections, serve client while True: try: cl, addr = s.accept() print('client connected from', addr) request = cl.recv(1024) print("request:") print(request) request = str(request) led_on = request.find('led=on') led_off = request.find('led=off') print( 'led on = ' + str(led_on)) print( 'led off = ' + str(led_off)) if led_on == 8: print("led on") led.value(1) if led_off == 8: print("led off") led.value(0) ledState = "LED is OFF" if led.value() == 0 else "LED is ON" # a compact if-else statement if button.value() == 1: # button not pressed print("button NOT pressed") buttonState = "Button is NOT pressed" else: print("button pressed") buttonState = "Button is pressed" # Create and send response stateis = ledState + " and " + buttonState response = html % stateis cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') cl.send(response) cl.close() except OSError as e: cl.close() print('connection closed') #https://core-electronics.com.au/guides/raspberry-pi-pico-w-create-a-simple-http-server/ #https://www.youtube.com/watch?v=AK8UYh7pMGM&t=515s Part 3 # Good graphical interface on HTTP surver # Control an LED and read a Button using a web browser import time import network import socket from machine import Pin led = Pin(15, Pin.OUT) ledState = 'LED State Unknown' button = Pin(16, Pin.IN, Pin.PULL_UP) ssid = 'xxxxx' password = 'xxxxx' wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) # replace the "html" variable with the following to create a more user-friendly control panel html = """

Control Panel









%s

""" # Wait for connect or fail max_wait = 10 while max_wait > 0: if wlan.status() < 0 or wlan.status() >= 3: break max_wait -= 1 print('waiting for connection...') time.sleep(1) # Handle connection error if wlan.status() != 3: raise RuntimeError('network connection failed') else: print('Connected') status = wlan.ifconfig() print( 'ip = ' + status[0] ) # Open socket addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(1) print('listening on', addr) # Listen for connections, serve client while True: try: cl, addr = s.accept() print('client connected from', addr) request = cl.recv(1024) print("request:") print(request) request = str(request) led_on = request.find('led=on') led_off = request.find('led=off') print( 'led on = ' + str(led_on)) print( 'led off = ' + str(led_off)) if led_on == 8: print("led on") led.value(1) if led_off == 8: print("led off") led.value(0) ledState = "LED is OFF" if led.value() == 0 else "LED is ON" # a compact if-else statement if button.value() == 1: # button not pressed print("button NOT pressed") buttonState = "Button is NOT pressed" else: print("button pressed") buttonState = "Button is pressed" # Create and send response stateis = ledState + " and " + buttonState response = html % stateis cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') cl.send(response) cl.close() except OSError as e: cl.close() print('connection closed') #https://core-electronics.com.au/guides/raspberry-pi-pico-w-create-a-simple-http-server/ #https://www.youtube.com/watch?v=AK8UYh7pMGM&t=515s

CoreElectronics 1: Connecting to the Internet

# A simple example that: # - Connects to a WiFi Network defined by "ssid" and "password" # - Performs a GET request (loads a webpage) # - Queries the current time from a server import network # handles connecting to WiFi import urequests # handles making and servicing network requests import utime # Connect to network wlan = network.WLAN(network.STA_IF) wlan.active(True) # Fill in your network name (ssid) and password here: ssid = 'xxxxx' password = 'xxxxx' wlan.connect(ssid, password) # Example 1. Make a GET request for google.com and print HTML # Print the html content from google.com print("1. Querying google.com:") r = urequests.get("http://www.google.com") print(r.content) r.close() # Example 2. urequests can also handle basic json support! Let's get the current time from a server print("\n\n2. Querying the current GMT+0 time:") r = urequests.get("http://date.jsontest.com") # Server that returns the current GMT+0 time. #print the tuple print(r.json()) # Get the 'date and time' value from the JSON response date_str = r.json()['date'] time_str = r.json()['time'][:-3] # Remove the last 3 characters (' PM' or ' AM') # Print only the time part print("Today's date GMT+0 is ", date_str.split(' ')) print("Current time GMT+0 is ",time_str.split(' ')) #Adding [0] will remove the [AM/PM] string #https://core-electronics.com.au/guides/raspberry-pi-pico-w-connect-to-the-internet/ #https://www.youtube.com/watch?v=GiT3MzRzG48&t=306s

Wednesday, 19 April 2023

TTB07: Pico W LESSON 7: Controlling 3 LED with a Potentiometer in Micropython

from machine import Pin, ADC from time import sleep potPin = 28 greenLED = 3 yellowLED = 9 redLED = 15 myPot = ADC(potPin) myGreen = Pin(greenLED,Pin.OUT) myYellow = Pin(yellowLED,Pin.OUT) myRed = Pin(redLED,Pin.OUT) while True: potVal = myPot.read_u16() myVal=(100/65103)*potVal-(100*432/65103) print(myVal) sleep(.1) if myVal<80: myGreen.value(1) myYellow.value(0) myRed.value(0) if myVal>=80: myGreen.value(0) myYellow.value(1) myRed.value(0) if myVal>=95: myGreen.value(0) myYellow.value(0) myRed.value(1) Modification using compound conditional statement: if myVal>=80 and myVal<95: myGreen.value(0) myYellow.value(1) myRed.value(0)

Friday, 10 March 2023

TTB06: Using 'IF' statement (NO potentiometer)

from machine import Pin import time led=Pin(15,Pin.OUT) while True: CMD=input('What is your Command? (ON/OFF/TOGGLE) ') if CMD=='ON': led.value(1) if CMD=='OFF': led.value(0) if CMD=='TOGGLE' : led.toggle()

TTB06a: Using 'IF' statements with a Potentiometer in RPi Pico

from machine import Pin from time import sleep potPin = 28 myPot = machine.ADC(potPin) ledR = Pin(2,Pin.OUT) ledY = Pin(4,Pin.OUT) ledG = Pin(15,Pin.OUT) try: while True: potVal = myPot.read_u16() percentage = (-100/65262)*potVal-(-100/65262*272)+100 if percentage<80: ledG.value(1) ledY.value(0) ledR.value(0) if percentage>=80 and percentage <95: ledG.value(0) ledY.value(1) ledR.value(0) if percentage>=95: ledG.value(0) ledY.value(0) ledR.value(1) print(int(percentage)) sleep(0.5) except KeyboardInterrupt: ledR.value(0) ledY.value(0) ledG.value(0) print('LEDs are off, Goodbye')

Saturday, 4 March 2023

Loads of leds (random display)

from picozero import LED from time import sleep as zzz from random import randint #max of 16 LEDs if using picozero, due to PWM conflicts. #Using 0 to 15 to make setup easier num_leds = 8 overflow_num=2**num_leds leds=[] for i in range(0,num_leds): leds.append(LED(i)) #shuffle LEDs for j in range(num_leds): i=randint(1,num_leds-1) leds[j],leds[i]=leds[i],leds[j] #reverse the order of i and j try: while True: i=randint(0,overflow_num) for j, led in enumerate(leds): led.brightness=int(bin(i+overflow_num)[j+3]) zzz(3) except KeyboardInterrupt: for led in leds: led.off() print('Turn off LEDs and quit') #https://www.youtube.com/watch?v=9MZGQmkeLAA

TTB05: Reading Analog Voltages Using a Potentiometer

import machine from time import sleep potPin = 28 myPot = machine.ADC(potPin) while True: potVal=myPot.read_u16() #print('PotVal =',potVal) #max=65535, min=1024, middle=65535-1024=64511 voltage = (3.3/64511)*potVal - (1024*3.3/64511) #print('Voltage =',voltage) percentage = (100/64511)*potVal-(1024*100/64511) #print('Percentage = ',int(percentage)) print('PotVal =',potVal, 'Voltage =',voltage, 'Percentage = ',int(percentage)) sleep(1) #https://www.youtube.com/watch?v=ODWwErH_iGA&list=PLGs0VKk2DiYz8js1SJog21cDhkBqyAhC5&index=5

TTB04a: Creating a Knight Rider LED display using a Pi Pico W

from machine import Pin from time import sleep for i in range (8,14): Pin((i), Pin.OUT) delay = 0.2 kr=[8,0,8,9,0,9,10,0,10,11,0,11,12,0,12,13,0,13] def KITT(): for j in range(len(kr)): if kr[j] == 0: sleep(delay) else: Pin(kr[j]).toggle() try: while True: KITT() except KeyboardInterrupt: for k in range(8,14): Pin(k).value(0) print('Keboard Interrupted, LEDs off') #https://www.youtube.com/watch?v=cGgDzVtppaQ

TTB04: Create a Binary Counter Using the Pico W

from machine import Pin from time import sleep led1 = Pin(15, Pin.OUT) led2 = Pin(14, Pin.OUT) led3 = Pin(13, Pin.OUT) led4 = Pin(12, Pin.OUT) while True: led1.value(0) led2.value(0) led3.value(0) led4.value(0) sleep(.5) led1.value(0) led2.value(0) led3.value(0) led4.value(1) sleep(.5) led1.value(0) led2.value(0) led3.value(1) led4.value(0) sleep(.5) led1.value(0) led2.value(0) led3.value(1) led4.value(1) sleep(.5) led1.value(0) led2.value(1) led3.value(0) led4.value(0) sleep(.5) led1.value(0) led2.value(1) led3.value(0) led4.value(1) sleep(.5) led1.value(0) led2.value(1) led3.value(1) led4.value(0) sleep(.5) led1.value(0) led2.value(1) led3.value(1) led4.value(1) sleep(.5) led1.value(1) led2.value(0) led3.value(0) led4.value(0) sleep(.5) led1.value(1) led2.value(0) led3.value(0) led4.value(1) sleep(.5) led1.value(1) led2.value(0) led3.value(1) led4.value(0) sleep(.5) led1.value(1) led2.value(0) led3.value(1) led4.value(1) sleep(.5) led1.value(1) led2.value(1) led3.value(0) led4.value(0) sleep(.5) led1.value(1) led2.value(1) led3.value(0) led4.value(1) sleep(.5) led1.value(1) led2.value(1) led3.value(1) led4.value(0) sleep(.5) led1.value(1) led2.value(1) led3.value(1) led4.value(1) sleep(.5) https://www.youtube.com/watch?v=P1dzHNgAtvg&list=PLGs0VKk2DiYz8js1SJog21cDhkBqyAhC5&index=4

Saturday, 25 February 2023

1 more Easy raspberrypi Pico Projects (Morse Code)

from machine import Pin, PWM from time import sleep # Create a dictionary of Morse Code. s is for Short (or dots), l is for Long (or dashes) MorseCodes = { ' ': '', 'a': 'sl', 'b': 'lsss', 'c': 'lsls', 'd': 'lss', 'e': 's', 'f': 'ssls', 'g': 'lls', 'h': 'ssss', 'i': 'ss', 'j': 'slll', 'k': 'lsl', 'l': 'slss', 'm': 'll', 'n': 'ls', 'o': 'lll', 'p': 'slls', 'q': 'llsl', 'r': 'sls', 's': 'sss', 't': 'l', 'u': 'ssl', 'v': 'sssl', 'w': 'sll', 'x': 'lssl', 'y': 'lsll', 'z': 'llss', '1': 'sllll', '2': 'sslll', '3': 'sssll', '4': 'ssssl', '5': 'sssss', '6': 'lssss', '7': 'llsss', '8': 'lllss', '9': 'lllls', '0': 'lllll'} button = Pin(10, Pin.IN, Pin.PULL_UP) shortled = Pin(11, Pin.OUT) longled = Pin(12, Pin.OUT) speaker = PWM(Pin(13)) fast = 0.1 slow = 0.2 sound = True light = True pitch = 600 volume = 1500 speaker.freq(600) #pitch of sound. Higher number is higher pitch shortled.low() longled.low() def letterlookup(stringvalue): for k in MorseCodes: if MorseCodes[k] == stringvalue: return k return " " def blinkletter(letter): if letter != "": currentletter = MorseCodes[letter] if letter == " ": sleep(0.6) return print(letter + " : " + currentletter) for c in currentletter: if (c == 'l'): blinkspeed = slow if (c =='s'): blinkspeed = fast if light : shortled.high() if sound : speaker.freq(pitch) speaker.duty_u16(volume) sleep(blinkspeed) if light : shortled.low() if sound : speaker.duty_u16(0) sleep(blinkspeed) sleep(0.6) def playmessage(message): for c in message: blinkletter(str.lower(c)) def recordmessage(): print("start typing your Morse Code message! Wait 5 seconds to exit") TimeCount = 0 DelayCount = 0 CurrentLetter = "" CurrentWord = "" PreviousStatus = 1 while True: if button.value() == 0: #Button being pressed DelayCount = 0 TimeCount += 1 if TimeCount <= 15: #Its a short press! shortled.high() longled.low() elif TimeCount > 15: #Its a Long Press! shortled.low() longled.high() if PreviousStatus != button.value(): speaker.duty_u16(1500) elif button.value() == 1: #Button not being pressed speaker.duty_u16(0) shortled.low() longled.low() if TimeCount > 0: if TimeCount <= 15: CurrentLetter = CurrentLetter + "s" elif TimeCount > 15: CurrentLetter = CurrentLetter + "l" TimeCount = 0 DelayCount = DelayCount + 1 if DelayCount > 60: if CurrentLetter != "": CurrentWord = CurrentWord + letterlookup(CurrentLetter) CurrentLetter = "" print(CurrentWord) if DelayCount == 300: #Add a space CurrentWord = CurrentWord + " " if DelayCount == 500: print("You recorded " + CurrentWord) print("Exiting recording mode") return sleep(0.01) #After execute the program, on the shell, type {playmessage('SOS')} #https://www.youtube.com/watch?v=k2thJQxO17w #https://github.com/printnplay/Pico-MicroPython/blob/main/MorseCodeCreator.py

2 Easy raspberrypi Pico Projects (temperature and music)

from machine import ADC from time import sleep tempsensor = ADC(4) conversion_factor = 3.3 / (65535) # Conversion from Pin read to proper voltage while True: currentvoltage = tempsensor.read_u16() * conversion_factor temp = 27 - ((currentvoltage - 0.706)/0.001721) print(str(currentvoltage) + " : " + str(temp)) sleep(2) #https://www.youtube.com/watch?v=k2thJQxO17w #https://github.com/printnplay/Pico-MicroPython/blob/main/ReadTemp.py ======================================================================== from machine import PWM, Pin from time import sleep MusicNotes = {"B0": 31, "C1": 33,"CS1": 35,"D1": 37,"DS1": 39,"E1": 41,"F1": 44,"FS1": 46,"G1": 49,"GS1": 52,"A1": 55,"AS1": 58,"B1": 62, "C2": 65,"CS2": 69,"D2": 73,"DS2": 78,"E2": 82,"F2": 87,"FS2": 93,"G2": 98,"GS2": 104,"A2": 110,"AS2": 117,"B2": 123,"C3": 131,"CS3": 139, "D3": 147,"DS3": 156,"E3": 165,"F3": 175,"FS3": 185,"G3": 196,"GS3": 208,"A3": 220,"AS3": 233,"B3": 247,"C4": 262,"CS4": 277,"D4": 294, "DS4": 311,"E4": 330,"F4": 349,"FS4": 370,"G4": 392,"GS4": 415,"A4": 440,"AS4": 466,"B4": 494,"C5": 523,"CS5": 554,"D5": 587,"DS5": 622, "E5": 659,"F5": 698,"FS5": 740,"G5": 784,"GS5": 831,"A5": 880,"AS5": 932,"B5": 988,"C6": 1047,"CS6": 1109,"D6": 1175,"DS6": 1245,"E6": 1324, "F6": 1397,"FS6": 1480,"G6": 1568,"GS6": 1661,"A6": 1760,"AS6": 1865,"B6": 1976,"C7": 2093,"CS7": 2217,"D7": 2349,"DS7": 2489,"E7": 2637, "F7": 2794,"FS7": 2960,"G7": 3136,"GS7": 3322,"A7": 3520,"AS7": 3729,"B7": 3951,"C8": 4186,"CS8": 4435,"D8": 4699,"DS8": 4978} mario = ["E7", "E7", "0", "E7", "0", "C7", "E7", "0", "G7", "0", "0", "0", "G6", "0", "0", "0", "C7", "0", "0", "G6", "0", "0", "E6", "0", "0", "A6", "0", "B6", "0", "AS6", "A6", "0", "G6", "E7", "0", "G7", "A7", "0", "F7", "G7", "0", "E7", "0","C7", "D7", "B6", "0", "0", "C7", "0", "0", "G6", "0", "0", "E6", "0", "0", "A6", "0", "B6", "0", "AS6", "A6", "0", "G6", "E7", "0", "G7", "A7", "0", "F7", "G7", "0", "E7", "0","C7", "D7", "B6", "0", "0"] hotcrossedbuns = ["E3", "0", "D3", "0", "C3", "0", "S", "E3", "0", "D3", "0", "C3", "0", "S", "C3", "C3", "C3", "C3", "D3", "D3", "D3", "D3", "S", "E3", "0", "D3", "0", "C3", "0"] speaker = PWM(Pin(13)) blueled = Pin(12, Pin.OUT) redled = Pin(11, Pin.OUT) def playnote(Note, Duration): if Note == "0": sleep(Duration) if Note == "S": speaker.duty_u16(0) sleep(Duration) elif Note != "0": speaker.duty_u16(0) sleep(0.05) blueled.toggle() redled.toggle() speaker.duty_u16(1500) speaker.freq(MusicNotes[Note]) sleep(Duration) blueled.high() redled.low() for c in hotcrossedbuns: playnote(c, 0.2) sleep(1) for c in mario: playnote(c, 0.4) speaker.duty_u16(0) #https://www.youtube.com/watch?v=k2thJQxO17w #https://github.com/printnplay/Pico-MicroPython/blob/main/MusicTest.py

Wednesday, 22 February 2023

Exercise 1 using TM1637

#Library import utime import tm1637 from machine import Pin #setup tm = tm1637.TM1637(clk=machine.Pin(4), dio=machine.Pin(5)) #to clear the display tm.show(' ') utime.sleep(2) #to present data on the display #here are the examples tm.number(1) utime.sleep(2) tm.number(12) utime.sleep(2) tm.number(123) utime.sleep(2) tm.number(1234) utime.sleep(2) #notice that the display starts from right to left tm.show("AbCd") utime.sleep(2) #Show scrolling text #delay controls the speed of the scrolling text. #The default delay is set as 250ms tm.scroll("8888 8888 8888", delay=300) utime.sleep(2) #to display time with the colon seperating hour and minutes tm.numbers(12,30) utime.sleep(2) #Temperature #Display with the degree Celsius sign #it accepts 2 digit +ve no. or a single digit with -ve no. tm.temperature(37) utime.sleep(2) #digital clock while True: time=utime.localtime() print(time) #(2023, 2, 6, 20, 14, 57, 0, 37) # 0 1 2 3 4 5 6 7 hr=time[3] mn=time[4] tm.numbers(hr,mn) utime.sleep(15) tm.show(' ') #if you open up tm1637.py you can see the functions number() and numbers() #in this library #From SSGoh on Github #Also From: https://microcontrollerslab.com/7-segment-display-raspberry-pi-pico/

Tuesday, 21 February 2023

Stop watch using TM1637

from machine import Pin import time import tm1637 tm = tm1637.TM1637(clk=Pin(4), dio=Pin(5)) Button = Pin(2, Pin.IN) a = 0 b = 0 tm.show(' ') tm.numbers(a,b) while True: if Button.value() == 1: while True: tm.numbers(a,b) b = b+1 time.sleep(1) if b > 59: b = 0 a = a+1 time.sleep(1) #Stop watch using Raspberry Pi Pico #https://electricdiylab.com/stopwatch-using-raspberry-pi-pico/#Schematic_Diagram

Thursday, 9 February 2023

Morse Code - SOS

from machine import Pin from time import sleep # the message to send in morse-code messages = ["sos sos sos sos sos sos"] #timing for encoding beatLen = 0.15 #length of dits and dahs ditLen = beatLen * 1 dahLen = beatLen * 3 #assign the signal lamp pin and amke sure its off signalLamp = Pin(15, Pin.OUT) signalLamp.off() #according to stories I've heard... hellNotFrozen = True #table to transform readable letters into morse code code = {'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-'} while hellNotFrozen: #loop through the list of messages for message in messages: # more code only has uppercase message = message.upper().strip() #loop through each character in the message for xmitChr in message: print(xmitChr, end = '') # show off the character on if xmitChr == ' ': # space means end of word # end of word - wait 4 more beats (7 total) sleep(beatLen * 4) else:# encode a character #loop through the ditdah code for this character for ditDah in code[xmitChr]: #turn the signal lamp on and count beats signalLamp.on() if ditDah == ".": sleep(ditLen) else: sleep(dahLen) # turn off signal lamp and 1 beat pause between signalLamp.off() sleep(beatLen) # end of character - wait for 2 more beats (3 in total) sleep(beatLen * 2) #end of message - wait 4 more beats (7 in total) sleep(beatLen * 4) print() #https://www.youtube.com/watch?v=cCSYFUeO-CU Observe the video for 'short short short', 'long long long', 'short short short'