This blog is a record of what I do, what I think and what I read. If you are interested to know about my professional experience, you can refer to the other blog of mine. http://admi2camac.blogspot.com/
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
Subscribe to:
Posts (Atom)