Skip to content

5. INPUT & OUTPUT DEVICES

RGB LED (OUTPUT)

RGB
17 Red, 18 Green, and 19 Blue

To get yellow light, we must combine red and green. In the code below, 17 is used for red and 18 for green.

from machine import Pin
import time

RGBLED = Pin(17, Pin.OUT)
RGBLED2 = Pin(18, Pin.OUT)
RGBLED.value(1)
RGBLED2.value(1)
time.sleep(2)
RGBLED.value(0)
RGBLED2.value(0)

yellow

Combining red and blue lights produces magenta.

from machine import Pin
import time

RGBLED = Pin(17, Pin.OUT)
RGBLED2 = Pin(19, Pin.OUT)
RGBLED.value(1)
RGBLED2.value(1)
time.sleep(2)
RGBLED.value(0)
RGBLED2.value(0)

pink

Combining green and blue lights produces cyan.

from machine import Pin
import time

RGBLED = Pin(18, Pin.OUT)
RGBLED2 = Pin(19, Pin.OUT)
RGBLED.value(1)
RGBLED2.value(1)
time.sleep(2)
RGBLED.value(0)
RGBLED2.value(0)

cyan

Combining all light colors produces white.

from machine import Pin
import time

RGBLED = Pin(17, Pin.OUT)
RGBLED2 = Pin(18, Pin.OUT)
RGBLED3 = Pin(19, Pin.OUT)
RGBLED.value(1)
RGBLED2.value(1)
RGBLED3.value(1)
time.sleep(2)
RGBLED.value(0)
RGBLED2.value(0)
RGBLED3.value(0)

white

SENSORS (INPUT)

This week, we started constructing circuits for sensors.

We began with temperature and humidity, soil moisture, and water level sensors.
1

Water Level Sensor

Water level sensor code link: Water Level Sensor Tutorial

Circuit example configuration:
2

Our circuit:
3

Our working code:

codeSM

you can copy the code here

//#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  // lcd.init();
  // lcd.backlight();
}
void loop() {
  int value = analogRead(A0);
  // lcd.setCursor(0, 0);
  // lcd.print("Value :");
  // lcd.print(value);
  // lcd.print("   ");
//  Serial.println(value);

  // lcd.setCursor(0, 1);
  // lcd.print("W Level :");

if (value == 0){ 
//   //   lcd.print("Empty ");
Serial.println("empty");
 } else if (value > 1 && value < 200) {
//   //   lcd.print("LOW   ");
Serial.println("Low");
} else if (value > 201 && value < 220) {
//   //   lcd.print("Medium");
Serial.println("Medium");
} else if (value > 220){
//   //   lcd.print("HIGH  ");
Serial.println("High");
 }
}

Humidity and Temperature Sensor

We used example code for this sensor that is already available in the Arduino

Circuit example configuration:
4

Our circuit:
5

Our working code:

you can copy the code here

/**
 * DHT11 Sensor Reader
 * This sketch reads temperature and humidity data from the DHT11 sensor and prints the values to the serial port.
 * It also handles potential error states that might occur during reading.
 *
 * Author: Dhruba Saha
 * Version: 2.1.0
 * License: MIT
 */

// Include the DHT11 library for interfacing with the sensor.
#include <DHT11.h>

// Create an instance of the DHT11 class.
// - For Arduino: Connect the sensor to Digital I/O Pin 2.
// - For ESP32: Connect the sensor to pin GPIO2 or P2.
// - For ESP8266: Connect the sensor to GPIO2 or D4.
DHT11 dht11(2);

void setup() {
    // Initialize serial communication to allow debugging and data readout.
    // Using a baud rate of 9600 bps.
    Serial.begin(9600);

    // Uncomment the line below to set a custom delay between sensor readings (in milliseconds).
    // dht11.setDelay(500); // Set this to the desired delay. Default is 500ms.
}

void loop() {
    int temperature = 0;
    int humidity = 0;

    // Attempt to read the temperature and humidity values from the DHT11 sensor.
    int result = dht11.readTemperatureHumidity(temperature, humidity);

    // Check the results of the readings.
    // If the reading is successful, print the temperature and humidity values.
    // If there are errors, print the appropriate error messages.
    if (result == 0) {
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.print(" °C\tHumidity: ");
        Serial.print(humidity);
        Serial.println(" %");
    } else {
        // Print error message based on the error code.
        Serial.println(DHT11::getErrorString(result));
    }
}

Soil Moisture Sensor

Circuit example configuration:
6

Our circuit:
7

codeSM

you can copy the code here

// Sensor pins
#define sensorPower 7
#define sensorPin A1

void setup() {
    pinMode(sensorPower, OUTPUT);

    // Initially keep the sensor OFF
    digitalWrite(sensorPower, LOW);

    Serial.begin(9600);
}

void loop() {
    // Get the reading from the function below and print it
    int sensorValue = readSensor();
    Serial.print("Analog output: ");
    Serial.println(sensorValue);

    if (sensorValue == 0) {
        Serial.println("no soil detected");
    } 
    else if (sensorValue > 0 && sensorValue < 500) {
        Serial.println("too wet");
    } 
    else if (sensorValue > 500 && sensorValue < 750) {
        Serial.println("target range");
    } 
    else if (sensorValue > 750) {
        Serial.println("dry needs water");
    }

    delay(1000);
}

// This function returns the analog soil moisture measurement
int readSensor() {
    digitalWrite(sensorPower, HIGH);    // Turn the sensor ON
    delay(10);                          // Allow power to settleS
    int val = analogRead(sensorPin);    // Read the analog value from sensor
    digitalWrite(sensorPower, LOW);     // Turn the sensor OFF
    return val;                         // Return analog moisture value
}

Other Sensors we used 8

LDR light Dependant Resistor


Last update: August 1, 2024