Merge pull request #3073 from adafruit/soil_sensor

simple soil sensor examples
This commit is contained in:
Liz 2025-07-10 09:08:02 -04:00 committed by GitHub
commit e9b900c317
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 134 additions and 0 deletions

View file

@ -0,0 +1,53 @@
// SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT
/* micro:bit Advanced Soil Sensor Demo */
#include <Adafruit_Microbit.h>
Adafruit_Microbit_Matrix microbit;
int sensorPin = 0;
int onPin = 2;
int moisture = 0;
const uint8_t
frown_bmp[] =
{ B00000,
B01010,
B00000,
B01110,
B10001, };
const uint8_t
smile_bmp[] =
{ B00000,
B01010,
B00000,
B10001,
B01110, };
void setup() {
Serial.begin(115200);
while ( !Serial ) delay(10);
Serial.println("micro:bit advanced soil sensor");
microbit.begin();
pinMode(onPin, OUTPUT);
}
void loop() {
digitalWrite(onPin, HIGH);
delay(50);
// read the value from the sensor:
moisture = analogRead(sensorPin);
Serial.print("Soil moisture: ");
Serial.println(moisture);
if (moisture > 200) {
microbit.show(smile_bmp);
} else {
microbit.show(frown_bmp);
}
digitalWrite(onPin, LOW);
delay(5000);
}

View file

@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT
/* micro:bit Simple Soil Sensor Demo */
#include <Adafruit_Microbit.h>
Adafruit_Microbit_Matrix microbit;
int sensorPin = 0;
int moisture = 0;
const uint8_t
frown_bmp[] =
{ B00000,
B01010,
B00000,
B01110,
B10001, };
const uint8_t
smile_bmp[] =
{ B00000,
B01010,
B00000,
B10001,
B01110, };
void setup() {
Serial.begin(115200);
while ( !Serial ) delay(10);
Serial.println("micro:bit simple soil sensor");
microbit.begin();
}
void loop() {
// read the value from the sensor:
moisture = analogRead(sensorPin);
Serial.print("Soil moisture: ");
Serial.println(moisture);
if (moisture > 200) {
microbit.show(smile_bmp);
} else {
microbit.show(frown_bmp);
}
delay(5000);
}

View file

@ -0,0 +1,18 @@
# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# pylint: disable=wildcard-import, undefined-variable
"""MicroPython Advanced Soil Sensing Demo for micro:bit"""
from microbit import *
while True:
pin2.write_digital(1)
sleep(100)
moisture = pin0.read_analog()
if moisture > 200:
display.show(Image.HAPPY)
else:
display.show(Image.SAD)
pin2.write_digital(0)
sleep(5000)

View file

@ -0,0 +1,15 @@
# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# pylint: disable=wildcard-import, undefined-variable
"""MicroPython Simple Soil Sensing Demo for micro:bit"""
from microbit import *
while True:
moisture = pin0.read_analog()
if moisture > 200:
display.show(Image.HAPPY)
else:
display.show(Image.SAD)
sleep(5000)