6. Input & Output device¶
For this week, more applications were used on the microcontrollers available at the lab for both input and output equipment which were followed in the tasks below.
First individual task¶
First individual task is to use output equipment and activate them using Arduino.
Create beeping sounds using Arduino¶
A buzzer is a tiny electromechanical device that is frequently used to create sound in electrical circuits, particularly those that use microcontrollers. It usually consists of a coil and a diaphragm, which, when an electrical current flows through the coil, vibrates to produce sound waves that may be heard. Digital or PWM (Pulse Width Modulation) signals can be used to drive a buzzer in microcontroller applications, like those utilizing Arduino or other platforms, to produce various melodies or tones. It is a crucial part of many electronic systems and gadgets since its main purpose is to warn or notify consumers using sound. It serves applications including timers, alarms, and user interface feedback.
Code below was applied to the buzzer used to create the desired beeping sounds as an example of an application.
// Define the buzzer pin
#define BUZZER_PIN 13
# The digital pin 13 is referenced by the constant BUZZER_PIN, which is defined in this line. The buzzer will be attached to this.
void setup() {
// No setup is needed for tone function
}
# When the program first launches, the setup() function is called once. Since the tone() function doesn't need any initial setting, there aren't any setup instructions in this instance.
void loop() {
// Generate a 1 kHz beep for 500 ms
tone(13, 1000, 500);
// Wait for 1 second
delay(1000);
// Generate a 2 kHz beep for 300 ms
tone(13, 2000, 300);
// Wait for 1 second
delay(1000);
# After setup(), the loop() function constantly executes. Within this loop:
# First Tone: tone(13, 1000, 500); causes pin 13 to produce a sound at 1 kHz (1000 Hz) for 500 ms.
# Delay: delay(1000); causes a 1000 ms (1 s) pause in the program.
# Second Tone: tone(13, 2000, 300); produces a sound on pin 13 at 2 kHz (2000 Hz) for 300 ms.
# Delay: Another 1000-second delay that pauses the program for one second before the loop is repeated.
# Summary: Through the use of a buzzer, the software continuously generates two alternating tones: a 1 kHz tone for 500 ms, followed by a 1-second pause, and a 2 kHz tone for 300 ms, followed by another 1-second pause. There is no end to this loop.
}
Recording below proofs that the code is successful.
Activating 130-DC fan motor using Arduino¶
A 130 DC fan motor microcontroller regulates the speed and function of a fan motor in various cooling applications. It uses pulse width modulation (PWM) signals and temperature or humidity sensors to control speed and improve performance. The circuit may include driver components to manage current needs and protect the microcontroller from electrical surges. This configuration is suitable for automatic cooling systems in computers, appliances, and HVAC systems.
This part wil cover the coding steps for activating a 130-DC fan motor which were extracted from an this website and it will be uploaded to Arduino.
Proof for code working as shown below.
/* 130 DC Motor
by DFRobot <https:www.dfrobot.com>
*/
# This part serves as a comment that identifies the creator (DFRobot) and the code's purpose.
int motorPin = 13; //Motor drive pin D3
int motorSpeed; //Define motor speed
# motorPin: Pin 13, which controls the motor, is the value of this variable.
# motorSpeed: This variable, which ranges from 0 (off) to 255 (maximum speed), will store the motor current speed.
void setup()
{
Serial.begin(9600);
}
# To enable communication with a computer or other devices, the setup() method sets up serial communication at a baud rate of 9600 bps.
void loop()
{
for(motorSpeed = 0 ; motorSpeed <= 255; motorSpeed+=5)
{
analogWrite(motorPin, motorSpeed); //PWM speed control
delay(30);
}
for(motorSpeed = 255 ; motorSpeed >= 0; motorSpeed-=5)
{
analogWrite(motorPin, motorSpeed); //PWM speed control
delay(30);
}
}
# After setup(), the loop() method executes continually. There are two for loops in it:
# Increasing Speed: Using 5-increment increments, the first for loop progressively raises the motor speed from 0 to 255.
# AnalogWrite(motorPin, motorSpeed); controls the motor speed by sending motorPin a PWM signal.
# delay(30); allows the motor speed to smoothly adjust by pausing for 30 milliseconds.
# Decreasing Speed: In increments of 5, the second for loop progressively reduces the motor speed from 255 to 0.
# The motor speed is controlled throughout this phase using the same analogWrite() and delay() routines.
#__Summary__: This code repeatedly repeats the cycle of smoothly ramping a DC motor up to full speed and then back down to off. By adjusting the duty cycle of the signal delivered to the motor, PWM enables precise control over the motor speed.
Adjusting an RGB light using Arduino¶
RGB LED microcontrollers are compact electronic devices that control the color and brightness of RGB LEDs, consisting of red, green, and blue light-emitting diodes. They use PWM signals to adjust intensity, create a wide spectrum of colors, and respond to external inputs. They are versatile in modern electronics and visual design.
For this part, an RGB light is adjusted to shift between rainbow colors using the code below.
// Define pin numbers for RGB LED
const int redPin = 12;
const int greenPin = 13;
const int bluePin = 14;
# The pins that are connected to the RGB LED's red, green, and blue components are defined in this section as constants. Pins 12, 13, and 14 are utilized.
// Number of colors in the rainbow
const int numColors = 7;
// Define colors in RGB format
int rainbowColors[numColors][3] = {
{255, 0, 0}, // Red
{255, 127, 0}, // Orange
{255, 255, 0}, // Yellow
{0, 255, 0}, // Green
{0, 0, 255}, // Blue
{75, 0, 130}, // Indigo
{148, 0, 211} // Violet
# The entire number of colors in the rainbow is specified by numColors.
# The red, green, and blue intensity values (0 to 255) are contained in each sub-array of rainbowColors, a 2D array that defines each color in RGB format.
};
void setup() {
// Set RGB pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
# When the program first launches, the setup() function is called once. PinMode() is used to set up the RGB LED pins as output pins.
}
void loop() {
// Cycle through rainbow colors
for (int i = 0; i < numColors; i++) {
setColor(rainbowColors[i][0], rainbowColors[i][1], rainbowColors[i][2]);
delay(500); // Change color every 500 milliseconds
}
# After setup(), the loop() method executes continually. It has a for loop that iterates over the rainbowColors array colors.
# Before switching to the next color, it waits 500 milliseconds after calling the setColor() function to set the RGB LED to the current color for each color.
}
// Function to set the color of the RGB LED
void setColor(int red, int green, int blue) {
// Set PWM values for each color
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
# The setColor() function utilizes analogWrite() to set the PWM values for each pin after receiving three parameters: the red, green, and blue intensities. This enables the RGB LED to show the desired color by regulating the brightness of each color component.
}
Recording for the RGB light shifting between colors is shown below.
A fading effect was applied to the rainbow RGB light shift using the code below.
// Define pin numbers for RGB LED
const int redPin = 12;
const int greenPin = 13;
const int bluePin = 14;
// Function to set the color of the RGB LED
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
// Function to create a fading effect
void fadeToColor(int targetRed, int targetGreen, int targetBlue, int duration) {
// Starting color values
int currentRed = 0;
int currentGreen = 0;
int currentBlue = 0;
// Fade to target color
for (int i = 0; i <= 255; i++) {
currentRed = (targetRed * i) / 255;
currentGreen = (targetGreen * i) / 255;
currentBlue = (targetBlue * i) / 255;
setColor(currentRed, currentGreen, currentBlue);
delay(duration / 255); // Adjust this to control fading speed
}
}
void setup() {
// Set RGB pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// Fade through rainbow colors with increased duration
fadeToColor(255, 0, 0, 2000); // Red
fadeToColor(255, 127, 0, 2000); // Orange
fadeToColor(255, 255, 0, 2000); // Yellow
fadeToColor(0, 255, 0, 2000); // Green
fadeToColor(0, 0, 255, 2000); // Blue
fadeToColor(75, 0, 130, 2000); // Indigo
fadeToColor(148, 0, 211, 2000); // Violet
}
Second individual task¶
For this task, two more equipment will be operated using Arduino IDE and be merged in as shown below. For all the applications, a potentiometer is used as an input equipment.
A potentiometer microcontroller is a device that uses a potentiometer to measure and control voltage levels in an electronic circuit. It consists of a three-terminal component with two fixed resistors and a movable wiper. The output voltage is read through an analog-to-digital converter, allowing the microcontroller to interpret user adjustments. This functionality is commonly used in volume control, brightness adjustment, and user input interfaces, making it popular in audio equipment, lighting controls, and robotics.
Adjusting RGB blue color light using Potentiometer¶
// Define pins
const int potPin = 4; // Analog input pin connected to the potentiometer
const int bluePin = 14; // PWM output pin connected to the blue pin of the RGB LED
// Variables
int potValue = 0; // Variable to store the potentiometer value (0-1023)
int pwmValue = 0; // Variable to store the mapped PWM value (0-255)
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Set the blue pin as output
pinMode(bluePin, OUTPUT);
// Inform the user
Serial.println("Blue LED Brightness Control Initialized!");
}
void loop() {
// Read the potentiometer value (0 to 1023)
potValue = analogRead(potPin);
// Map the potentiometer value to PWM range (0 to 255)
pwmValue = map(potValue, 0, 1023, 0, 255);
// Debugging: Print potentiometer and PWM values to the Serial Monitor
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" -> PWM Value: ");
Serial.println(pwmValue);
// Apply the PWM value to the blue pin of the RGB LED
analogWrite(bluePin, pwmValue);
// Small delay for stability
delay(10);
}
# Code Explanation:
# Pin Definitions
# const int potPin = 4;: This line defines potPin as the analog input pin (pin 4) connected to the potentiometer. The potentiometer will provide a variable voltage based on its position, which the Arduino reads as an analog value.
# const int bluePin = 14; This line defines bluePin as the PWM output pin (pin 14) is connected to the blue LED of the RGB LED. PWM (Pulse Width Modulation) allows for varying the brightness of the LED by changing the duty cycle of the signal sent to it.
# Variable Declarations:
# int potValue = 0; This variable stores the value read from the potentiometer, which ranges from 0 to 1023.
# int pwmValue = 0; This variable stores the mapped PWM value, which will range from 0 to 255, suitable for controlling the brightness of the LED.
# Setup Function:
# void setup() { ... } This function runs once when the program starts.
# Serial.begin(9600);: Initializes serial communication at a baud rate of 9600 for debugging purposes, allowing the Arduino to send data to the Serial Monitor.
# pinMode(bluePin, OUTPUT);: Sets the bluePin as an output pin, enabling it to send signals to the blue LED.
# Serial.println("Blue LED Brightness Control Initialized!"); Sends a message to the Serial Monitor indicating that the setup is complete.
Loop Function:
# void loop() { ... } This function runs continuously after the setup.
# potValue = analogRead(potPin); Reads the analog value from the potentiometer, which can range from 0 (minimum resistance) to 1023 (maximum resistance).
# pwmValue = map(potValue, 0, 1023, 0, 255);: Maps the potentiometer value from its range (0-1023) to the PWM range (0-255). This is necessary because the analogWrite() function requires a value between 0 and 255 to control the brightness of the LED.
# The next few lines print the potentiometer and PWM values to the Serial Monitor for debugging, allowing the user to see the current readings.
# analogWrite(bluePin, pwmValue);: Applies the mapped PWM value to the blue LED, adjusting its brightness based on the potentiometer position.
# delay(10);: Introduces a small delay (10 milliseconds) to stabilize the readings and prevent excessive serial output.
Video of the code application:
Adjusting 130DC fan motor using Potentiometer¶
const int potPin = 4; // Analog pin connected to the potentiometer
const int motorPin = 14; // PWM pin connected to the transistor base
# potPin: The analog pin (pin 4) that the potentiometer is attached to is defined by this constant.
# motorPin: This constant designates pin 14, the PWM pin that uses a transistor to regulate the motor speed.
void setup() {
pinMode(motorPin, OUTPUT); // Set motor pin as output
Serial.begin(115200); // Start serial communication for debugging
}
# pinMode(motorPin, OUTPUT): Sets motorPin up as an output so that it can communicate with the motor.
# In order to debug and monitor values in the Serial Monitor, Serial.begin(115200) initializes serial connection at a baud rate of 115200.
void loop() {
// Read the potentiometer value
int potValue = analogRead(potPin);
// Map the potentiometer value (0-1023) to PWM range (0-255)
int motorSpeed = map(potValue, 0, 1023, 0, 255);
// Print the potentiometer and motor speed values to the Serial Monitor
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" | Motor Speed: ");
Serial.println(motorSpeed);
// Set the motor speed using PWM
analogWrite(motorPin, motorSpeed);
// Small delay for stability
delay(100);
}
# The potentiometer analog value, which ranges from 0 to 1023, is read using the formula int potValue = analogRead(potPin);.
# Charting the Value:
# The formula int motorSpeed = map(potValue, 0, 1023, 0, 255);: transforms the potentiometer value into a PWM value that may be used to control motor speed.
# Serial Output
# or tracking purposes, the code outputs to the Serial Monitor the current potentiometer value and the associated motor speed.
# AnalogWrite(motorPin, motorSpeed);: This code controls the motor speed by sending a PWM signal to the motor that is dependent on the input from the potentiometer.
# Delay
# delay(100); To stabilize readings and avoid flooding the Serial Monitor with data too soon, a 100 ms delay is introduced.
# Summary: Using a potentiometer as an input device, this code efficiently controls a motor speed while delivering real-time feedback via serial connection.
Video of code:
Adjusting buzzer beeping level using Potentiometer¶
const int potPin = 4; // Analog pin connected to the potentiometer
const int buzzerPin = 13; // PWM pin connected to the buzzer
# potPin: The analog pin (pin 4) that the potentiometer is linked to is identified by this constant.
# buzzerPin: The PWM pin (pin 13) that controls the buzzer is identified by this constant.
void setup() {
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as an output
Serial.begin(115200); // Start serial communication for debugging
}
# pinMode(buzzerPin, OUTPUT): Sets the buzzer pin to function as an output, enabling the buzzer to receive signals.
# In order to enable the display of values in the Serial Monitor for debugging, Serial.begin(115200) initializes serial connection at a baud rate of 115200.
void loop() {
// Read the potentiometer value
int potValue = analogRead(potPin);
// Map the potentiometer value (0-1023) to a frequency range (e.g., 100-2000 Hz)
int frequency = map(potValue, 0, 100, 1023, 2000);
// Print the potentiometer and frequency values to the Serial Monitor
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" | Frequency: ");
Serial.println(frequency);
// Generate a tone on the buzzer
tone(buzzerPin, frequency);
// Small delay for stability
delay(100);
}
# Reading the Potentiometer: int potValue = analogRead(potPin);: This retrieves the potentiometer analog value, which is between 0 and 1023.
# Using int frequency = map(potValue, 0, 100, 1023, 2000) to map the value assigns a frequency range to the potentiometer value. However, since the potentiometer value ranges from 0 to 1023, it appears that the mapping settings are off. To correctly translate the potentiometer entire range to the desired frequency range, it should probably be mapped to (potValue, 0, 1023, 100, 2000).
# Serial Output: For tracking purposes, the code outputs to the Serial Monitor the generated frequency and the current potentiometer value.
# Creating a Tone: tone(frequency, buzzerPin);: produces a buzzer tone at the designated frequency.
# Delay: delay(100);: To stabilize readings and avoid over-stuffing the Serial Monitor with data, a 100 ms delay is introduced.
# Summary: With the help of this code, you can use a potentiometer to adjust a buzzer frequency while receiving real-time feedback via serial connection. Adjusting the sound frequency according to the potentiometer position is the planned feature; nevertheless, for correct functioning, a mapping issue needs to be fixed.
Video of code: