├── README.md ├── pwm-led-rpi ├── diagram.png └── pwm-led-rpi.py ├── gps-rpi-pico ├── pi pico gps 1.png └── gps-rpi-pico.py ├── gps2-rpi-pico ├── wiring-diagram.png └── gps2-rpi-pico.py ├── sim800l-relay ├── connection diagram.png └── sim800l-relay.py └── servo ├── servo-test.py └── servo.py /README.md: -------------------------------------------------------------------------------- 1 | # raspberry-pi-pico-micropython -------------------------------------------------------------------------------- /pwm-led-rpi/diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmadlogs/rpi-pico-upy/HEAD/pwm-led-rpi/diagram.png -------------------------------------------------------------------------------- /gps-rpi-pico/pi pico gps 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmadlogs/rpi-pico-upy/HEAD/gps-rpi-pico/pi pico gps 1.png -------------------------------------------------------------------------------- /gps2-rpi-pico/wiring-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmadlogs/rpi-pico-upy/HEAD/gps2-rpi-pico/wiring-diagram.png -------------------------------------------------------------------------------- /sim800l-relay/connection diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmadlogs/rpi-pico-upy/HEAD/sim800l-relay/connection diagram.png -------------------------------------------------------------------------------- /pwm-led-rpi/pwm-led-rpi.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, PWM 2 | from time import sleep 3 | 4 | pwm = PWM(Pin(15)) 5 | 6 | pwm.freq(1000) 7 | 8 | while True: 9 | for duty in range(0, 65025): 10 | pwm.duty_u16(duty) 11 | sleep(0.0001) 12 | 13 | for duty in range(65025, 0, -1): 14 | pwm.duty_u16(duty) 15 | sleep(0.0001) -------------------------------------------------------------------------------- /servo/servo-test.py: -------------------------------------------------------------------------------- 1 | from servo import Servo 2 | import utime 3 | 4 | pwmPin=15 #change for your pin 5 | motor=Servo(pwmPin) 6 | 7 | while True: 8 | motor.move(0) 9 | utime.sleep(1) 10 | motor.move(45) 11 | utime.sleep(1) 12 | motor.move(90) 13 | utime.sleep(1) 14 | motor.move(135) 15 | utime.sleep(1) 16 | motor.move(180) 17 | utime.sleep(1) 18 | -------------------------------------------------------------------------------- /servo/servo.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, PWM 2 | class Servo: 3 | # these defaults work for the standard TowerPro SG90 4 | __servo_pwm_freq = 50 5 | __min_u16_duty = 600 # offset for correction 6 | __max_u16_duty = 6000 # offset for correction 7 | min_angle = 0 8 | max_angle = 180 9 | current_angle = 0.001 10 | 11 | 12 | def __init__(self, pin): 13 | self.__initialise(pin) 14 | 15 | 16 | def update_settings(self, servo_pwm_freq, min_u16_duty, max_u16_duty, min_angle, max_angle, pin): 17 | self.__servo_pwm_freq = servo_pwm_freq 18 | self.__min_u16_duty = min_u16_duty 19 | self.__max_u16_duty = max_u16_duty 20 | self.min_angle = min_angle 21 | self.max_angle = max_angle 22 | self.__initialise(pin) 23 | 24 | 25 | def move(self, angle): 26 | # round to 2 decimal places, so we have a chance of reducing unwanted servo adjustments 27 | angle = round(angle, 2) 28 | # do we need to move? 29 | if angle == self.current_angle: 30 | return 31 | self.current_angle = angle 32 | # calculate the new duty cycle and move the motor 33 | duty_u16 = self.__angle_to_u16_duty(angle) 34 | self.__motor.duty_u16(duty_u16) 35 | 36 | 37 | def __angle_to_u16_duty(self, angle): 38 | return int((angle - self.min_angle) * self.__angle_conversion_factor) + self.__min_u16_duty 39 | 40 | 41 | def __initialise(self, pin): 42 | self.current_angle = -0.001 43 | self.__angle_conversion_factor = (self.__max_u16_duty - self.__min_u16_duty) / (self.max_angle - self.min_angle) 44 | self.__motor = PWM(Pin(pin)) 45 | self.__motor.freq(self.__servo_pwm_freq) -------------------------------------------------------------------------------- /gps2-rpi-pico/gps2-rpi-pico.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, UART, I2C 2 | #Import utime library to implement delay 3 | import utime, time 4 | 5 | #________________________________________________________ 6 | from ssd1306 import SSD1306_I2C 7 | #https://github.com/stlehmann/micropython-ssd1306 8 | #________________________________________________________ 9 | from micropyGPS import MicropyGPS 10 | #https://github.com/inmcm/micropyGPS 11 | #________________________________________________________ 12 | 13 | ########################################################## 14 | #Oled I2C connection 15 | i2c=I2C(0, sda=Pin(8), scl=Pin(9), freq=400000) 16 | oled = SSD1306_I2C(128, 64, i2c) 17 | ########################################################## 18 | 19 | ########################################################## 20 | #GPS Module UART Connection 21 | gps_module = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5)) 22 | ########################################################## 23 | 24 | 25 | ########################################################## 26 | TIMEZONE = 5 27 | my_gps = MicropyGPS(TIMEZONE) 28 | ########################################################## 29 | 30 | ########################################################## 31 | def convert(parts): 32 | if (parts[0] == 0): 33 | return None 34 | 35 | data = parts[0]+(parts[1]/60.0) 36 | # parts[2] contain 'E' or 'W' or 'N' or 'S' 37 | if (parts[2] == 'S'): 38 | data = -data 39 | if (parts[2] == 'W'): 40 | data = -data 41 | 42 | data = '{0:.6f}'.format(data) # to 6 decimal places 43 | return str(data) 44 | ########################################################## 45 | 46 | ########################################################## 47 | while True: 48 | #_________________________________________________ 49 | #print(i2c.scan()) 50 | length = gps_module.any() 51 | if length>0: 52 | b = gps_module.read(length) 53 | for x in b: 54 | msg = my_gps.update(chr(x)) 55 | #_________________________________________________ 56 | latitude = convert(my_gps.latitude) 57 | longitude = convert(my_gps.longitude) 58 | #_________________________________________________ 59 | if (latitude == None and latitude == None): 60 | oled.fill(0) 61 | oled.text("No Data", 0, 0) 62 | oled.show() 63 | continue 64 | #_________________________________________________ 65 | t = my_gps.timestamp 66 | #t[0] => hours : t[1] => minutes : t[2] => seconds 67 | gpsTime = '{:02d}:{:02d}:{:02}'.format(t[0], t[1], t[2]) 68 | 69 | gpsdate = my_gps.date_string('long') 70 | speed = my_gps.speed_string('kph') #'kph' or 'mph' or 'knot' 71 | #_________________________________________________ 72 | print('Lat:', latitude) 73 | print('Lng:', longitude) 74 | #print('time:', gpsTime) 75 | #print('Date:', gpsdate) 76 | #print('speed:', speed) 77 | #_________________________________________________ 78 | oled.fill(0) 79 | oled.text('Lat:'+ latitude, 0, 0) 80 | oled.text('Lng:'+ longitude, 0, 12) 81 | oled.text('Speed:'+ speed, 0, 24) 82 | oled.text('Time:'+ gpsTime, 0, 36) 83 | oled.text(gpsdate, 0, 48) 84 | oled.show() 85 | #_________________________________________________ 86 | 87 | ########################################################## 88 | -------------------------------------------------------------------------------- /gps-rpi-pico/gps-rpi-pico.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, UART, I2C 2 | from ssd1306 import SSD1306_I2C 3 | 4 | #Import utime library to implement delay 5 | import utime, time 6 | 7 | #Oled I2C connection 8 | i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000) 9 | oled = SSD1306_I2C(128, 64, i2c) 10 | 11 | #GPS Module UART Connection 12 | gps_module = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5)) 13 | 14 | #print gps module connection details 15 | print(gps_module) 16 | 17 | #Used to Store NMEA Sentences 18 | buff = bytearray(255) 19 | 20 | TIMEOUT = False 21 | 22 | #store the status of satellite is fixed or not 23 | FIX_STATUS = False 24 | 25 | #Store GPS Coordinates 26 | latitude = "" 27 | longitude = "" 28 | satellites = "" 29 | gpsTime = "" 30 | 31 | 32 | #function to get gps Coordinates 33 | def getPositionData(gps_module): 34 | global FIX_STATUS, TIMEOUT, latitude, longitude, satellites, gpsTime 35 | 36 | #run while loop to get gps data 37 | #or terminate while loop after 5 seconds timeout 38 | timeout = time.time() + 8 # 8 seconds from now 39 | while True: 40 | gps_module.readline() 41 | buff = str(gps_module.readline()) 42 | #parse $GPGGA term 43 | #b'$GPGGA,094840.000,2941.8543,N,07232.5745,E,1,09,0.9,102.1,M,0.0,M,,*6C\r\n' 44 | #print(buff) 45 | parts = buff.split(',') 46 | 47 | #if no gps displayed remove "and len(parts) == 15" from below if condition 48 | if (parts[0] == "b'$GPGGA" and len(parts) == 15): 49 | if(parts[1] and parts[2] and parts[3] and parts[4] and parts[5] and parts[6] and parts[7]): 50 | print(buff) 51 | #print("Message ID : " + parts[0]) 52 | #print("UTC time : " + parts[1]) 53 | #print("Latitude : " + parts[2]) 54 | #print("N/S : " + parts[3]) 55 | #print("Longitude : " + parts[4]) 56 | #print("E/W : " + parts[5]) 57 | #print("Position Fix: " + parts[6]) 58 | #print("n sat : " + parts[7]) 59 | 60 | latitude = convertToDigree(parts[2]) 61 | # parts[3] contain 'N' or 'S' 62 | if (parts[3] == 'S'): 63 | latitude = -latitude 64 | longitude = convertToDigree(parts[4]) 65 | # parts[5] contain 'E' or 'W' 66 | if (parts[5] == 'W'): 67 | longitude = -longitude 68 | satellites = parts[7] 69 | gpsTime = parts[1][0:2] + ":" + parts[1][2:4] + ":" + parts[1][4:6] 70 | FIX_STATUS = True 71 | break 72 | 73 | if (time.time() > timeout): 74 | TIMEOUT = True 75 | break 76 | utime.sleep_ms(500) 77 | 78 | #function to convert raw Latitude and Longitude 79 | #to actual Latitude and Longitude 80 | def convertToDigree(RawDegrees): 81 | 82 | RawAsFloat = float(RawDegrees) 83 | firstdigits = int(RawAsFloat/100) #degrees 84 | nexttwodigits = RawAsFloat - float(firstdigits*100) #minutes 85 | 86 | Converted = float(firstdigits + nexttwodigits/60.0) 87 | Converted = '{0:.6f}'.format(Converted) # to 6 decimal places 88 | return str(Converted) 89 | 90 | 91 | while True: 92 | 93 | getPositionData(gps_module) 94 | 95 | #if gps data is found then print it on lcd 96 | if(FIX_STATUS == True): 97 | print("fix......") 98 | oled.fill(0) 99 | oled.text("Lat: "+latitude, 0, 0) 100 | oled.text("Lng: "+longitude, 0, 10) 101 | oled.text("No of Sat: "+satellites, 0, 20) 102 | oled.text("Time: "+gpsTime, 0, 30) 103 | oled.show() 104 | print(latitude) 105 | print(longitude) 106 | print(satellites) 107 | print(gpsTime) 108 | 109 | FIX_STATUS = False 110 | 111 | if(TIMEOUT == True): 112 | print("Request Timeout: No GPS data is found.") 113 | #-------------------------------------------------- 114 | #updated on 5-May-2022 115 | oled.fill(0) 116 | oled.text("No GPS data is found", 0, 0) 117 | oled.show() 118 | #-------------------------------------------------- 119 | TIMEOUT = False 120 | 121 | -------------------------------------------------------------------------------- /sim800l-relay/sim800l-relay.py: -------------------------------------------------------------------------------- 1 | from machine import UART, Pin 2 | 3 | #import time 4 | 5 | #Issue: uart.readline() doesn't read line properly 6 | #Solution: increase the timeout value to 3000ms or 4000ms 7 | gsm_module = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5), timeout=2000) 8 | gsm_buffer = '' 9 | 10 | ######################################################################## 11 | #this phone number is used to send commands 12 | # and receive response from the project 13 | #Note: Don't enter the SIM800L phone number here 14 | destination_phone = 'ENTER_YOUR_PHONE_NUMBER' 15 | ######################################################################## 16 | 17 | relay1 = Pin(2, Pin.OUT) 18 | relay2 = Pin(3, Pin.OUT) 19 | 20 | ######################################################################## 21 | def convert_to_string(buf): 22 | tt = buf.decode('utf-8').strip() 23 | return tt 24 | ######################################################################## 25 | 26 | ######################################################################## 27 | def do_action(msg): 28 | msg = msg.lower() 29 | #print('do_action: '+msg) 30 | if(msg.strip() == 'relay1 off'): 31 | relay1(0) 32 | send_sms('Relay1 is OFF') 33 | elif(msg.strip() == "relay1 on"): 34 | relay1(1) 35 | send_sms('Relay1 is ON') 36 | elif(msg.strip() == 'relay2 off'): 37 | relay2(0) 38 | send_sms('Relay2 is OFF') 39 | elif(msg.strip() == 'relay2 on'): 40 | print('do_action1: '+msg) 41 | relay2(1) 42 | send_sms('Relay2 is ON') 43 | ######################################################################## 44 | 45 | ######################################################################## 46 | def send_command(cmdstr, lines=1, msgtext=None): 47 | global gsm_buffer 48 | #___________________________________________________________________ 49 | print(cmdstr) 50 | cmdstr = cmdstr+'\r\n' 51 | #___________________________________________________________________ 52 | #Empty the serial buffer 53 | while gsm_module.any(): 54 | gsm_module.read() 55 | #___________________________________________________________________ 56 | #Send command to sim800l 57 | gsm_module.write(cmdstr) 58 | #___________________________________________________________________ 59 | #Only used while sending sms 60 | if msgtext: 61 | print(msgtext) 62 | gsm_module.write(msgtext) 63 | #___________________________________________________________________ 64 | #while gsm_module.any(): 65 | #pass 66 | #___________________________________________________________________ 67 | #read data comming from sim800l line by line 68 | buf=gsm_module.readline() #discard linefeed etc 69 | #print('discard linefeed:{}'.format(buf)) 70 | buf=gsm_module.readline() 71 | #print('next linefeed:{}'.format(buf)) 72 | #___________________________________________________________________ 73 | if not buf: 74 | return None 75 | result = convert_to_string(buf) 76 | #___________________________________________________________________ 77 | #if there are multiple lines of data comming from sim800l 78 | if lines>1: 79 | gsm_buffer = '' 80 | for i in range(lines-1): 81 | buf=gsm_module.readline() 82 | if not buf: 83 | return result 84 | #print(buf) 85 | buf = convert_to_string(buf) 86 | if not buf == '' and not buf == 'OK': 87 | gsm_buffer += buf+'\n' 88 | #___________________________________________________________________ 89 | return result 90 | ######################################################################## 91 | 92 | ######################################################################## 93 | def read_sms(sms_id): 94 | result = send_command('AT+CMGR={}\n'.format(sms_id),99) 95 | print(result) 96 | #___________________________________________________________________ 97 | if result: 98 | #NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN 99 | #+CMGR: "REC READ","+923001234567","","21/04/18,11:15:53+20" 100 | params=result.split(',') 101 | if params[0] == '': 102 | return None 103 | #NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN 104 | params2 = params[0].split(':') 105 | if not params2[0]=='+CMGR': 106 | return None 107 | #NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN 108 | number = params[1].replace('"',' ').strip() 109 | date = params[3].replace('"',' ').strip() 110 | time = params[4].replace('"',' ').strip() 111 | #print('gsm_buffer:'+gsm_buffer) 112 | return [number,date,time,gsm_buffer] 113 | #NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN 114 | ######################################################################## 115 | 116 | ######################################################################## 117 | def send_sms(msgtext): 118 | global gsm_buffer 119 | result = send_command('AT+CMGS="{}"\n'.format(destination_phone),99,msgtext+'\x1A') 120 | if result and result=='>' and gsm_buffer: 121 | params = gsm_buffer.split(':') 122 | if params[0]=='+CUSD' or params[0] == '+CMGS': 123 | print('OK') 124 | return 'OK' 125 | print('ERROR') 126 | return 'ERROR' 127 | ######################################################################## 128 | print(send_command('AT')) 129 | print(send_command('AT+CMGF=1')) 130 | print(send_command('AT+CNMI=1')) 131 | #______________________________________________________ 132 | #delete all sms from sim800l memory 133 | #print(send_command('AT+CMGD=1,4')) 134 | #______________________________________________________ 135 | #send ussd code 136 | #print(send_command('AT+CUSD=1,"*ENTER_YOUR_USSD_CODE#"')) 137 | #______________________________________________________ 138 | #read_sms(1) 139 | #send_sms('ddddd') 140 | 141 | ######################################################################## 142 | while True: 143 | if gsm_module.any(): 144 | buf=gsm_module.readline() 145 | buf = convert_to_string(buf) 146 | print(buf) 147 | 148 | params=buf.split(',') 149 | #if params[0] == "RING": 150 | #elif params[0][0:5] == "+CLIP": 151 | #elif params[0][0:5] == "+CUSD": 152 | #elif params[0] == "NO CARRIER": 153 | #NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN 154 | if params[0][0:5] == "+CMTI": 155 | msgid = int(params[1]) 156 | msg_data = read_sms(msgid) 157 | #______________________________________________________ 158 | if not msg_data: 159 | print("No sms data found.") 160 | break 161 | #______________________________________________________ 162 | #print(msg_data[0]) #Sender Phone Number 163 | #print(msg_data[1]) #Received SMS Date 164 | #print(msg_data[2]) #Received SMS Time 165 | print(msg_data[3]) #Received SMS Text 166 | #______________________________________________________ 167 | if not msg_data[0] == destination_phone: 168 | print("Destination phone pumber not matching") 169 | break 170 | #______________________________________________________ 171 | #function to turn ON or OFF relays 172 | do_action(msg_data[3]) 173 | #______________________________________________________ 174 | #NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN 175 | ######################################################################## 176 | --------------------------------------------------------------------------------