4. Embedded Programming¶
Using the ESP32 board I designed and milled, I controlled the RGB LED on it using a potentiometer.
A potentiometer is a manually adjustable variable resistor with 3 terminals. Two of the terminals are connected to the opposite ends of a resistive element, and the third terminal connects to a sliding contact, called a wiper, moving over the resistive element. To adjust the output voltage the sliding contact gets moved along the resistor on the output side.
Potentiometer Working¶
Code
const int potPin = 34; // Analog pin connected to the potentiometer
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int sensorValue = analogRead(potPin); // Read the potentiometer value
float voltage = sensorValue * (3.3 / 4095.0); // Convert ADC value to voltage (assuming 3.3V reference voltage)
Serial.print("Potentiometer Value: ");
Serial.print(sensorValue);
Serial.print(" | Voltage: ");
Serial.println(voltage, 3); // Print voltage value with 3 decimal places
delay(1000); // Wait for 1 second before reading again
}
Potentiometer + LED¶
Code
const int potPin = 4; // Analog pin connected to the potentiometer
const int ledPin = 13; // PWM pin connected to the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int sensorValue = analogRead(potPin); // Read the potentiometer value
int brightness = map(sensorValue, 0, 4095, 0, 255); // Map potentiometer value to LED brightness (0-255)
analogWrite(ledPin, brightness); // Set the LED brightness
Serial.print("Potentiometer Value: ");
Serial.print(sensorValue);
Serial.print(" | Brightness: ");
Serial.println(brightness);
delay(100); // Delay for stability
}
#include <Adafruit_NeoPixel.h>
#define LED_PIN 38 // Pin connected to the data input of the WS2813 LED
#define POT_PIN 4 // Analog pin connected to the potentiometer
Adafruit_NeoPixel led(1, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
led.begin();
led.show(); // Initialize LED to 'off'
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int sensorValue = analogRead(POT_PIN); // Read the potentiometer value
int brightness = map(sensorValue, 0, 4095, 0, 255); // Map potentiometer value to LED brightness (0-255)
led.setBrightness(brightness); // Set the brightness of the LED
// Set the color of the LED to red
led.setPixelColor(0, led.Color(255, 0, 0));
led.show(); // Update the LED
Serial.print("Potentiometer Value: ");
Serial.print(sensorValue);
Serial.print(" | Brightness: ");
Serial.println(brightness);
delay(100); // Delay for stability
}
Potentiometer + RGB LED WS2813¶
Code
#include <Adafruit_NeoPixel.h>
#define LED_PIN 38 // Pin connected to the data input of the WS2813 LED
#define POT_PIN 4 // Analog pin connected to the potentiometer
Adafruit_NeoPixel led(1, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
led.begin();
led.show(); // Initialize LED to 'off'
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int sensorValue = analogRead(POT_PIN); // Read the potentiometer value
// Calculate color components based on potentiometer value
int red, green, blue;
if (sensorValue < 2048) {
// Sunset to sunrise transition
red = map(sensorValue, 0, 2047, 255, 0); // Red decreases
green = map(sensorValue, 0, 2047, 150, 0); // Green decreases
blue = map(sensorValue, 0, 2047, 0, 255); // Blue increases
} else {
// Sunrise to sunset transition
red = map(sensorValue, 2048, 4095, 0, 255); // Red increases
green = map(sensorValue, 2048, 4095, 0, 150); // Green increases
blue = map(sensorValue, 2048, 4095, 255, 0); // Blue decreases
}
// Set the color of the LED
led.setPixelColor(0, led.Color(red, green, blue));
led.show(); // Update the LED
Serial.print("Potentiometer Value: ");
Serial.print(sensorValue);
Serial.print(" | Red: ");
Serial.print(red);
Serial.print(" | Green: ");
Serial.print(green);
Serial.print(" | Blue: ");
Serial.println(blue);
delay(100); // Delay for stability
}