Interactive Servo Control with Real-Time LED Feedback: An Arduino-Based Project
Project Introduction
This project is an Arduino-based application that utilizes a servo motor and an LED matrix display to create an interactive system. The servo's angle is controlled by a potentiometer, and the current angle is displayed on the LED matrix in real-time.
Project Principle
The core principle of this project is to demonstrate the integration of input from a potentiometer, processing through an Arduino microcontroller, and output via a servo motor and an LED matrix display. The potentiometer provides an analog signal that is converted into a digital signal by the Arduino. This digital signal is then mapped to a range suitable for the servo motor's movement. The LED matrix is used to display the current angle of the servo, providing visual feedback.
Project Setup Method
- Gather all necessary materials and components.
- Set up the circuit according to the schematic.
- Upload the provided code to the Arduino.
- Power the system and observe the real-time feedback on the LED matrix.
Materials Needed
- Arduino UNO board
- LED matrix display compatible with Arduino
- Servo motor
- Potentiometer
- Jumper wires
- Breadboard (optional, for prototyping)
- Power supply for the Arduino and peripherals
Circuit Setup
- Connect the servo motor to digital pin 9 of the Arduino.
- Connect the potentiometer to the analog pin A0 and ground it.
- Connect the LED matrix to the appropriate pins as per its documentation.
- Ensure all components are powered correctly.
Code
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include <Servo.h> // Include the Servo library
ArduinoLEDMatrix matrix;
Servo myservo; // Create a servo object
int potPin = A0; // Define the potentiometer input pin
int potValue = 0; // Variable to read the value from the potentiometer
int angle = 0; // Variable to store the angle for the servo
void setup() {
myservo.attach(9); // Attach the servo to digital pin 9
Serial.begin(9600); // Start serial communication at 9600bps
matrix.begin();
// Initialize the display with a welcome message
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
const char text[] = "UNO r4";
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText();
matrix.endDraw();
delay(2000);
}
void loop() {
potValue = analogRead(potPin); // Read the value from the potentiometer (0-1023)
angle = map(potValue, 0, 1023, 0, 180); // Map the potValue to a servo angle (0-180)
myservo.write(angle); // Set the servo to the angle
delay(15); // Wait for the servo to reach the position
// Update the display with the current servo angle
matrix.beginDraw();
String text = String(angle);
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText(NO_SCROLL);
matrix.endDraw();
}
Line-by-Line Code Explanation
- The code begins by including necessary libraries for the LED matrix and the Servo functionality.
- An instance of
ArduinoLEDMatrix
andServo
is created to manage the LED matrix and the servo motor, respectively. - The potentiometer is connected to analog pin A0 for reading its value.
- In the
setup()
function, the servo is attached to pin 9, serial communication is initiated, and the LED matrix is initialized. - A welcome message is displayed on the LED matrix using predefined font and color settings.
- The
loop()
function continuously reads the potentiometer's value, maps it to a servo angle, and writes this angle to the servo. - The current angle is then displayed on the LED matrix, providing real-time feedback.