Skip to main content

Analog IO

Overview​

This lab is a simple introduction to reading and outputting analog signals over the ESP32/Arduino pins. Please note that if you use an Arduino, you would have to use analog specific pins. If you want to challenge yourself, try completing the objectives below in order before referring to the example code!

Objectives​

  • Read analog values from photoresistor
  • Turn on LED or print something whenever the light level drops below a threshold
  • Add a delay so that the light level has to be below a threshold for a certain amount of time before the LED is turned on ⭐︎

Wiring Diagram​

PlatformIO Banner

Code Solutions​

#include <Arduino.h>

#define PHOTORESISTOR_PIN D25 // Defining photoresistor pin number
#define LED_PIN 15 // Defining LED pin number

long minLightLevel = 1000; // Defining low light threshold

void setup() {
Serial.begin(115200); // Initializing Serial with a baud rate of 115200
pinMode(LED_PIN, OUTPUT); // Setting pin mode of LED pin to output
}

void loop() {
long lightLevel = analogRead(PHOTORESISTOR_PIN); // Reading photoresistor pin as analog. This will return a numerical value
Serial.println(lightLevel); // Printing light level to serial

// Checking if light level is below threshold
if (lightLevel < minLightLevel) {
digitalWrite(LED_PIN, HIGH); // Setting LED state to on
} else {
digitalWrite(LED_PIN, LOW); // Setting LED state to off
}
}
note

The basic example code does not include a solution for the 3rd objective