Raspberry pi pico
Programación microPython
Sensores:
LDR
DHT22 (AM2302)
BMP180
18B20
OLED DS1302
Programación microPython
Sensores:
LDR
DHT22 (AM2302)
BMP180
18B20
OLED DS1302
En este artículo lo realizo con la Raspberry pi pico y programando en microPython.
Todo el montaje lo realizo en pasos, y grabado en vídeo, paso a paso.
El final del montaje queda de la siguiente forma:
El primer paso es colocar un led a modo de piloto de alimentación, y hacer parpadear el led que viene en la placa.
# SETA43 # seta43.duckdns.org # seta43.blogspot.com # Palencia / España # 31-08-2022 import machine import utime led = machine.Pin(25, machine.Pin.OUT) while True: led.value(0) utime.sleep(0.5) led.value(1) utime.sleep(0.5) |
En este paso colocamos un sensor de temperatura y humedad
El sensor es el DHT22 o su equivalente AM2302.
# SETA43 # seta43.duckdns.org # seta43.blogspot.com # Palencia / España # 31-08-2022 import machine import utime led = machine.Pin(25, machine.Pin.OUT) from DHT22 import DHT22 from machine import Pin dht_sensor=DHT22(Pin(15),dht11=False) while True: #--DHT22-------------------------------------------- T, H = dht_sensor.read() if T is None: print("Error en el sensor!") else: print("Sensor DHT22->","{}ºC {}%".format(T,H)) led.value(0) utime.sleep(0.5) led.value(1) utime.sleep(0.5) |
En este paso ponemos un sensor de luz tipo LDR, y leemos el valor mediante un conversor ADC.
# SETA43 # seta43.duckdns.org # seta43.blogspot.com # Palencia / España # 31-08-2022 import machine import utime led = machine.Pin(25, machine.Pin.OUT) from DHT22 import DHT22 from machine import Pin dht_sensor=DHT22(Pin(15),dht11=False) analog_value = machine.ADC(28) while True: #--DHT22-------------------------------------------- T, H = dht_sensor.read() if T is None: print("Error en el sensor!") else: print("Sensor DHT22->","{}ºC {}%".format(T,H)) #--LDR-------------------------------------------- reading = analog_value.read_u16() print("LDR-> ADC: ",reading) led.value(0) utime.sleep(0.5) led.value(1) utime.sleep(0.5) |
En este paso colocamos un sensor de presión que además dispone de un sensor de temperatura.
El sensor viene en un módulo BMP180, los datos se mandan mediante el bus I2C.
Con este sensor se puede calcular la altura.
# SETA43 # seta43.duckdns.org # seta43.blogspot.com # Palencia / España # 31-08-2022 import machine import utime from bmp180 import BMP180 from machine import I2C, Pin led = machine.Pin(25, machine.Pin.OUT) from DHT22 import DHT22 from machine import Pin dht_sensor=DHT22(Pin(15),dht11=False) analog_value = machine.ADC(28) i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=200000) bmp180 = BMP180(i2c) bmp180.oversample_sett = 2 baseline = bmp180.pressure/100 ALTURA = 790 print ("Doble sensor DHT22 y DS18x20") #Write in cmd found addresses i2cScan = i2c.scan() counter = 0 for i in i2cScan: print('I2C Address ' + str(counter) + ' : '+hex(i).upper()) print ("**************************") #--------------- while True: #--DHT22-------------------------------------------- T, H = dht_sensor.read() if T is None: print("Error en el sensor!") else: print("Sensor DHT22->","{}ºC {}%".format(T,H)) #--LDR-------------------------------------------- reading = analog_value.read_u16() print("LDR-> ADC: ",reading) #..BMP180--------------------------------------------- temperatura = bmp180.temperature presion = bmp180.pressure/100 alt2=44330.0* (1-pow(presion/baseline,1/5.255) ) alt2=ALTURA+alt2 alt2=round(alt2) temperatura=round(temperatura,1) presion=round(presion) print("Tem_BMP180=",temperatura,"ºC Presion=", presion," ALTITUD=",alt2) led.value(0) utime.sleep(0.5) led.value(1) utime.sleep(0.5) |
En este paso colocamos un sensor de temperatura 18B20.
Este chip se comunica mediante una sola línea de datos, y si se quiere se puede utilizar solo dos lineas, para hacer funcionar el sensor.
Además se pueden colocar varios chip en paralelo, que se podrán leer sus temperaturas independientemente.
El esquema solo utilizo un sensor, en la practica utilizo 2 chip con sus respectivas lecturas.
# SETA43 # seta43.duckdns.org # seta43.blogspot.com # Palencia / España # 31-08-2022 import machine import utime from bmp180 import BMP180 from machine import I2C, Pin import onewire, ds18x20, time led = machine.Pin(25, machine.Pin.OUT) from DHT22 import DHT22 from machine import Pin dht_sensor=DHT22(Pin(15),dht11=False) analog_value = machine.ADC(28) i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=200000) bmp180 = BMP180(i2c) bmp180.oversample_sett = 2 baseline = bmp180.pressure/100 ALTURA = 790 print ("Doble sensor DHT22 y DS18x20") #Write in cmd found addresses i2cScan = i2c.scan() counter = 0 for i in i2cScan: print('I2C Address ' + str(counter) + ' : '+hex(i).upper()) print ("**************************") #--------------- ds_pin = machine.Pin(22) ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin)) roms = ds_sensor.scan() while True: #--DHT22-------------------------------------------- T, H = dht_sensor.read() if T is None: print("Error en el sensor!") else: print("Sensor DHT22->","{}ºC {}%".format(T,H)) #--LDR-------------------------------------------- reading = analog_value.read_u16() print("LDR-> ADC: ",reading) #..BMP180--------------------------------------------- temperatura = bmp180.temperature presion = bmp180.pressure/100 alt2=44330.0* (1-pow(presion/baseline,1/5.255) ) alt2=ALTURA+alt2 alt2=round(alt2) temperatura=round(temperatura,1) presion=round(presion) print("Tem_BMP180=",temperatura,"ºC Presion=", presion," ALTITUD=",alt2) #--DS18B20-------------------------------------------- ds_sensor.convert_temp() time.sleep_ms(750) x=0 for rom in roms: #print ("Sensor=",x," ",rom," Temperatura=",ds_sensor.read_temp(rom)) print ("Sensor_DS18B20_nº",x," "," Temperatura=",ds_sensor.read_temp(rom)) x=x+1 print("---------------------") led.value(0) utime.sleep(0.5) led.value(1) utime.sleep(0.5) |
Por último colocamos un visualizardor tipo OLED , el modelo SSD1306.
Lo bueno de colocar este modelo es que se conecta en el bus I2C, lo que resulta muy práctico y sencillo.
# SETA43 # seta43.duckdns.org # seta43.blogspot.com # Palencia / España # 31-08-2022 import machine import utime from bmp180 import BMP180 from machine import I2C, Pin import onewire, ds18x20, time from DHT22 import DHT22 from machine import Pin from ssd1306 import SSD1306_I2C led = machine.Pin(25, machine.Pin.OUT) dht_sensor=DHT22(Pin(15),dht11=False) analog_value = machine.ADC(28) i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=200000) bmp180 = BMP180(i2c) bmp180.oversample_sett = 2 baseline = bmp180.pressure/100 ALTURA = 790 WIDTH = 128 # oled display width HEIGHT = 64 oled = SSD1306_I2C(WIDTH, HEIGHT, i2c) print ("Doble sensor DHT22 y DS18x20") #Write in cmd found addresses i2cScan = i2c.scan() counter = 0 for i in i2cScan: print('I2C Address ' + str(counter) + ' : '+hex(i).upper()) print ("**************************") #--------------- ds_pin = machine.Pin(22) ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin)) roms = ds_sensor.scan() while True: #--DHT22-------------------------------------------- T, H = dht_sensor.read() if T is None: print("Error en el sensor!") else: print("Sensor DHT22->","{}ºC {}%".format(T,H)) #--LDR-------------------------------------------- reading = analog_value.read_u16() print("LDR-> ADC: ",reading) #..BMP180--------------------------------------------- temperatura = bmp180.temperature presion = bmp180.pressure/100 alt2=44330.0* (1-pow(presion/baseline,1/5.255) ) alt2=ALTURA+alt2 alt2=round(alt2) temperatura=round(temperatura,1) presion=round(presion) print("Tem_BMP180=",temperatura,"ºC Presion=", presion," ALTITUD=",alt2) #--DS18B20-------------------------------------------- ds_sensor.convert_temp() time.sleep_ms(750) x=0 for rom in roms: #print ("Sensor=",x," ",rom," Temperatura=",ds_sensor.read_temp(rom)) print ("Sensor_DS18B20_nº",x," "," Temperatura=",ds_sensor.read_temp(rom)) x=x+1 oled.fill(0) oled.text("-Seta43- ",0,8) tmp=str(T)+" C DHT22" oled.text(tmp,0,16) tmp=str(H)+" % DHT22" oled.text(tmp,0,24) tmp=str(presion)+" mBa BMP180" oled.text(tmp,0,32) tmp=str(alt2)+ " m BMP180" oled.text(tmp,0,40) tmp=str(reading)+" luz LDR" oled.text(tmp,0,48) oled.show() print("---------------------") led.value(0) utime.sleep(0.5) led.value(1) utime.sleep(0.5) |
Como se puede entender, para el funcionamiento de todo, se hace uso de varias librerías.
Para que funcione las librerías deben ser copiadas a la raspberry pi pico.
PROGRAMAS Y LIBRERÍAS
VÍDEO
Espero que este montaje os haya parecido interesante.
Saludos.
Juan Galaz
Bibliografía:
https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf
https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf
seta_PIB
seta_PIA
https://lindevs.com/set-up-raspberry-pi-pico-sdk-on-ubuntu
https://www.electroniclinic.com/raspberry-pi-pico-rp2040-programming-in-micropython-with-examples/
https://www.youtube.com/watch?v=2gihPgEkvL8
https://picockpit.com/raspberry-pi/es/todo-sobre-la-frambuesa-pi-pico/
No hay comentarios:
Publicar un comentario