Week 06: Input & Output device¶
This week I worked on defining my final project idea and started to getting used to the documentation process.
Hardware Devices Used¶
Output Devices¶
An output device is a piece of computer hardware that receives data from a source and converts it into another format. Based on its function, an output device can transform data into forms such as audio, visual, or physical copies.
↪ RGB LED¶
An RGB LED is a module capable of producing nearly any color by combining three primary additive colors: red, green, and blue. The simplest form of an RGB LED contains three individual light-emitting diodes packaged together under a clear protective lens.
This code controls an RGB LED connected to an ESP32, creating smooth color transitions through the rainbow. It defines pins for red, green, and blue, and uses the setColor
function to adjust their brightness via PWM. The loop
function cycles through a series of for
loops, incrementally fading between colors by adjusting the RGB values, with small delays ensuring smooth transitions.
// Define the GPIO pins for the RGB LED
const int redPin = 12; // Replace with your ESP32 pin for Red
const int greenPin = 13; // Replace with your ESP32 pin for Green
const int bluePin = 14; // Replace with your ESP32 pin for Blue
// Function to set the color of the RGB LED
void setColor(int red, int green, int blue) {
analogWrite(redPin, red); // Set PWM for Red (0-255)
analogWrite(greenPin, green); // Set PWM for Green (0-255)
analogWrite(bluePin, blue); // Set PWM for Blue (0-255)
}
void setup() {
// Initialize the RGB LED pins as output
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Start with the LED off
setColor(0, 0, 0);
}
void loop() {
// Fade smoothly through the colors of the rainbow
for (int brightness = 0; brightness <= 255; brightness++) {
// Red to Orange
setColor(255, brightness, 0); // Increase green
delay(10);
}
for (int brightness = 0; brightness <= 255; brightness++) {
// Orange to Yellow
setColor(255 - brightness, 255, 0); // Decrease red
delay(10);
}
for (int brightness = 0; brightness <= 255; brightness++) {
// Yellow to Green
setColor(0, 255, brightness); // Increase blue
delay(10);
}
for (int brightness = 0; brightness <= 255; brightness++) {
// Green to Cyan
setColor(0, 255 - brightness, 255); // Decrease green
delay(10);
}
for (int brightness = 0; brightness <= 255; brightness++) {
// Cyan to Blue
setColor(brightness, 0, 255); // Increase red
delay(10);
}
for (int brightness = 0; brightness <= 255; brightness++) {
// Blue to Purple
setColor(255, 0, 255 - brightness); // Decrease blue
delay(10);
}
for (int brightness = 0; brightness <= 255; brightness++) {
// Purple to Pink
setColor(255, brightness, 255); // Increase green
delay(10);
}
for (int brightness = 0; brightness <= 255; brightness++) {
// Pink to Red
setColor(255, 255 - brightness, 255 - brightness); // Fade green and blue out
delay(10);
}
}
↪ Fan¶
A fan is a powered machine that creates airflow.
This code controls a DC motor’s speed and direction using an H-bridge motor driver connected to an ESP32. The motor speed is adjusted with PWM on the ENA
pin, and the direction is set by toggling the IN1
and IN2
pins. The loop
function gradually increases and decreases the motor’s speed in forward direction, pauses, and then stops the motor, with the controlMotor
function managing speed and direction, and stopMotor
ensuring the motor is completely stopped.
// Pin definitions
#define ENA 12 // PWM pin to control motor speed
#define IN1 13 // Input 1 for motor direction
#define IN2 14 // Input 2 for motor direction
// Speed settings (0-255)
double motorSpeed = 0;
double minSpeed = 0;
double maxSpeed = 255;
double inc = 10;
void setup() {
// Set motor control pins as outputs
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
// Start serial communication for debugging
Serial.begin(9600);
Serial.println("130 DC Fan Motor Control Initialized");
}
void loop() {
// Fade from low to slightly higher speed
for (motorSpeed = minSpeed; motorSpeed <= maxSpeed; motorSpeed += inc) {
controlMotor(motorSpeed, true); // Run forward
delay(100);
}
// Hold at medium-slow speed for 2 seconds
delay(2000);
// Gradually reduce speed back to low
for (motorSpeed = maxSpeed; motorSpeed >= minSpeed; motorSpeed -= inc) {
controlMotor(motorSpeed, true); // Run forward
delay(100);
}
// Pause motor for 2 seconds
stopMotor();
delay(2000);
}
// Function to control the motor
void controlMotor(int speed, bool forward) {
analogWrite(ENA, speed); // Set motor speed
if (forward) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
}
// Function to stop the motor
void stopMotor() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0); // Ensure PWM is 0
}
↪ Buzzer¶
A buzzer or beeper is an audio signaling device.
This code plays the melody of Twinkle, Twinkle, Little Star on a buzzer connected to an ESP32. The twinkleNotes
array defines the frequencies of the song’s notes in Hz, while twinkleDurations
specifies the duration of each note in milliseconds. The playTwinkleTwinkle
function iterates through the notes, using the tone
function to generate sound on the buzzer, with pauses between notes to create a smooth melody.
// Pin configurations
const int buzzerPin = 23; // GPIO pin for the buzzer
// Frequencies for the complete "Twinkle, Twinkle, Little Star" (in Hz)
int twinkleNotes[] = {
261, 261, 392, 392, 440, 440, 392, // Twinkle, twinkle, little star (C C G G A A G)
349, 349, 330, 330, 294, 294, 261, // How I wonder what you are (F F E E D D C)
392, 392, 349, 349, 330, 330, 294, // Up above the world so high (G G F F E E D)
392, 392, 349, 349, 330, 330, 294, // Like a diamond in the sky (G G F F E E D)
261, 261, 392, 392, 440, 440, 392, // Twinkle, twinkle, little star (C C G G A A G)
349, 349, 330, 330, 294, 294, 261 // How I wonder what you are (F F E E D D C)
};
// Durations for each note (in ms)
int twinkleDurations[] = {
500, 500, 500, 500, 500, 500, 1000, // C C G G A A G
500, 500, 500, 500, 500, 500, 1000, // F F E E D D C
500, 500, 500, 500, 500, 500, 1000, // G G F F E E D
500, 500, 500, 500, 500, 500, 1000, // G G F F E E D
500, 500, 500, 500, 500, 500, 1000, // C C G G A A G
500, 500, 500, 500, 500, 500, 1000 // F F E E D D C
};
void setup() {
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
Serial.begin(115200); // Debugging
playTwinkleTwinkle(); // Play the melody at startup
}
void loop() {
// No loop logic required
}
void playTwinkleTwinkle() {
int notesCount = sizeof(twinkleNotes) / sizeof(twinkleNotes[0]);
for (int i = 0; i < notesCount; i++) {
if (twinkleNotes[i] == 0) {
noTone(buzzerPin); // Pause
} else {
tone(buzzerPin, twinkleNotes[i], twinkleDurations[i]); // Play note
}
delay(twinkleDurations[i]); // Wait for the duration of the note
noTone(buzzerPin); // Small gap between notes
delay(50); // Short delay between notes
}
}
☆ How to add libraires?¶
Follow these steps to install a library and include it to your code.
Input Devices¶
There are many different ways we can input data into a computer system. The devices we use are called input devices and include keyboards, touch screens, microphones, movement sensors and webcams.
↪ Potentiometer¶
A potentiometer is used as a voltage divider or variable resistor in a circuit. Some applications include dimmer switches for lights, brightness controls in televisions, and faders in audio equipment I took the code from this website.
This code reads analog input from pin 4
(GPIO 4) and displays the value on the Serial Monitor. In the setup
function, serial communication is initialized at a baud rate of 9600. The loop
function continuously reads the analog value, prints it to the monitor, and pauses for 100 milliseconds to ensure stability.
// Runs once when the board is reset or powered on
void setup() {
// Initialize serial communication at 9600 baud
Serial.begin(9600);
}
// Runs repeatedly in a loop
void loop() {
// Read the input value from analog pin A0
int sensorValue = analogRead(4);
// Print the read value to the Serial Monitor
Serial.println(sensorValue);
// Short delay for stability (optional, 1 ms is usually negligible)
delay(100);
}
↪ IR Receiver Sensor¶
This sensor is an electronic device that employs infrared radiation to detect and measure ambient light levels. It is possible to integrate an IR remote controller with Arduino or Raspberry Pi in order to control devices such as televisions, audio systems, lighting controls, and other home electronic appliances. I took the code from this website.
This code uses the IRremote library to decode signals from an IR remote control using an IR receiver connected to pin 13
of an ESP32. In the setup
function, serial communication is initialized, and the IR receiver is activated. The loop
function continuously checks for incoming IR signals, decodes the button press, prints its hexadecimal code to the Serial Monitor, and prepares the receiver for the next signal.
#include <IRremote.hpp>
const int RECV_PIN = 13; // Connect the IR receiver's OUT pin here
IRrecv irrecv(RECV_PIN);
void setup() {
Serial.begin(9600); // Start Serial Monitor
irrecv.begin(RECV_PIN); // Start IR receiver
Serial.println("Point the remote at the receiver and press buttons...");
}
void loop() {
if (irrecv.decode()) {
// Print the button code in HEX format
Serial.print("Button Code: 0x");
Serial.println(irrecv.decodedIRData.decodedRawData, HEX);
// Prepare for the next signal
irrecv.resume();
}
}
An Input + An Output¶
↪ Potentiometer + RGB LED¶
↝ Result¶
↝ Hardware Used¶
↝ Code¶
I took the code from this website.
This code controls an RGB LED’s color based on the position of a potentiometer connected to pin 4
. The potentiometer value is mapped to a range of 0–1535 to represent a smooth color transition through red, blue, and green phases. Depending on the mapped value, the code calculates the intensity of each LED color, creating a gradient effect as the potentiometer is rotated, and adjusts the LED brightness using PWM on pins 12
, 13
, and 14
.
#define RGB_RED_PIN 12
#define RGB_BLUE_PIN 13
#define RGB_GREEN_PIN 14
#define POTENTIOMETER_PIN 4
void setup()
{
pinMode(RGB_RED_PIN, OUTPUT);
pinMode(RGB_BLUE_PIN, OUTPUT);
pinMode(RGB_GREEN_PIN, OUTPUT);
}
void loop()
{
int potentiometerValue = analogRead(POTENTIOMETER_PIN);
int rgbValue = map(potentiometerValue, 0, 1023, 0, 1535);
int red;
int blue;
int green;
if (rgbValue < 256) {
red = 255;
blue = rgbValue;
green = 0;
}
else if (rgbValue < 512) {
red = 511 - rgbValue;
blue = 255;
green = 0;
}
else if (rgbValue < 768) {
red = 0;
blue = 255;
green = rgbValue - 512;
}
else if (rgbValue < 1024) {
red = 0;
blue = 1023 - rgbValue;
green = 255;
}
else if (rgbValue < 1280) {
red = rgbValue - 1024;
blue = 0;
green = 255;
}
else {
red = 255;
blue = 0;
green = 1535 - rgbValue;
}
analogWrite(RGB_RED_PIN, red);
analogWrite(RGB_BLUE_PIN, blue);
analogWrite(RGB_GREEN_PIN, green);
}
↪ IR Receiver + RGB LED + Buzzer¶
Loading..