Sistema completo de monitorizacion y control: lee humedad del suelo y nivel de luz, ventila cuando hay exceso de calor y enciende el LED si hay poca luz. Dashboard en tiempo real en la OLED.
Este proyecto utiliza el mayor numero de componentes hasta ahora — 5 a la vez. Todos se conectan a la placa de expansion simultaneamente.
Con 5 componentes, la organizacion del cableado es clave. Usa colores diferentes para cada componente y etiqueta los cables si es posible.
Codigo completo con comentarios. El sistema lee sensores, toma decisiones automaticas y actualiza el dashboard cada segundo. El boton A guarda y muestra los valores maximos registrados.
// Invernadero automatizado con múltiples sensores
// Sistema: sensor → decisión → actuador
let humedad = 0
let luz = 0
let UMBRAL_LUZ_BAJA = 300 // Encender LED artificial
let UMBRAL_CALOR = 700 // Encender ventilador
// Inicializar OLED
OLED.init(128, 64)
function getEstadoHumedad(val: number): string {
if (val < 200) return "Seco"
if (val < 500) return "Normal"
return "Humedo"
}
function getEstadoLuz(val: number): string {
if (val < 300) return "Oscuro"
if (val < 700) return "Normal"
return "Brillante"
}
basic.forever(function () {
// === LEER SENSORES ===
humedad = pins.analogReadPin(AnalogPin.P0)
luz = pins.analogReadPin(AnalogPin.P1)
// === CONTROL AUTOMÁTICO ===
// Ventilador: encender si hay mucha luz (calor)
if (luz > UMBRAL_CALOR) {
pins.analogWritePin(AnalogPin.P2, Math.map(luz, UMBRAL_CALOR, 1023, 400, 1023))
} else {
pins.analogWritePin(AnalogPin.P2, 0) // Ventilador apagado
}
// LED artificial: encender si hay poca luz
if (luz < UMBRAL_LUZ_BAJA) {
pins.digitalWritePin(DigitalPin.P3, 1)
} else {
pins.digitalWritePin(DigitalPin.P3, 0)
}
// === DASHBOARD OLED ===
OLED.clear()
OLED.writeString("=== INVERNADERO ===")
OLED.writeString("Hum: " + humedad + " " + getEstadoHumedad(humedad))
OLED.writeString("Luz: " + luz + " " + getEstadoLuz(luz))
OLED.writeString("Vent: " + (luz > UMBRAL_CALOR ? "ON" : "OFF"))
OLED.writeString("LED: " + (luz < UMBRAL_LUZ_BAJA ? "ON" : "OFF"))
basic.pause(1000)
})
// Botón A: mostrar valores máximos registrados
let maxHumedad = 0
let maxLuz = 0
input.onButtonPressed(Button.A, function () {
if (humedad > maxHumedad) maxHumedad = humedad
if (luz > maxLuz) maxLuz = luz
OLED.clear()
OLED.writeString("=== MAXIMOS ===")
OLED.writeString("Max Hum: " + maxHumedad)
OLED.writeString("Max Luz: " + maxLuz)
basic.pause(3000)
})
from microbit import *
import ssd1306
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
UMBRAL_LUZ_BAJA = 300
UMBRAL_CALOR = 700
max_humedad = 0
max_luz = 0
def estado_humedad(val):
if val < 200: return "Seco"
if val < 500: return "Normal"
return "Humedo"
def estado_luz(val):
if val < 300: return "Oscuro"
if val < 700: return "Normal"
return "Brillante"
def mapear(v, amin, amax, bmin, bmax):
return int((v - amin) * (bmax - bmin) / (amax - amin) + bmin)
while True:
# Leer sensores
humedad = pin0.read_analog()
luz = pin1.read_analog()
# Actualizar máximos
if humedad > max_humedad: max_humedad = humedad
if luz > max_luz: max_luz = luz
# Control ventilador
if luz > UMBRAL_CALOR:
vel = mapear(luz, UMBRAL_CALOR, 1023, 400, 1023)
pin2.write_analog(vel)
else:
pin2.write_analog(0)
# Control LED artificial
pin3.write_digital(1 if luz < UMBRAL_LUZ_BAJA else 0)
# Dashboard OLED
oled.fill(0)
oled.text("== INVERNADERO ==", 0, 0)
oled.text("Hum:" + str(humedad) + " " + estado_humedad(humedad), 0, 14)
oled.text("Luz:" + str(luz) + " " + estado_luz(luz), 0, 26)
oled.text("Vent:" + ("ON" if luz > UMBRAL_CALOR else "OFF"), 0, 38)
oled.text("LED:" + ("ON" if luz < UMBRAL_LUZ_BAJA else "OFF"), 0, 50)
oled.show()
# Botón A: ver máximos
if button_a.was_pressed():
oled.fill(0)
oled.text("=== MAXIMOS ===", 0, 0)
oled.text("Max Hum: " + str(max_humedad), 0, 20)
oled.text("Max Luz: " + str(max_luz), 0, 36)
oled.show()
sleep(3000)
sleep(1000)
En vez de encender el ventilador a tope o apagarlo, usamos Math.map (MakeCode) o la funcion mapear (MicroPython) para convertir el rango de luz (700-1023) al rango de PWM del ventilador (400-1023). Asi el ventilador va mas rapido cuanta mas luz hay — un control proporcional real, como el que usan los sistemas industriales.
Con 5 componentes hay mas cosas que verificar. Prueba cada uno por separado antes de probar el sistema completo.
Retos para convertir este invernadero basico en un sistema mas completo. Algunos requieren investigacion y experimentacion.
Con mas componentes, hay mas posibles puntos de fallo. Aislacion: prueba cada componente de uno en uno.
pins.digitalWritePin(DigitalPin.P3, 1) por un valor fijo durante la depuracion para confirmar que el LED responde.function getEstadoHumedad(...) al nivel raiz del archivo.El invernadero puede evolucionar hacia un sistema de precision agricola real. Estas ideas van de menor a mayor complejidad.