10. Output Devices¶
Understanding the basics¶
What are Output devices?
Output devices are components that take signals from a microcontroller or computer and turn them into actions. These actions can be light, sound, movement, heat, or display.
Types of Onput devices
Visual Output Devices (Show Light or Images)
-
LEDs – Light-emitting diodes that turn on/off or change brightness.
-
LCD/OLED Displays – Show text, images, or graphics.
-
Projectors – Display visuals on a larger screen.
Motion Output Devices (Create Movement)
-
Servo Motors – Move in precise angles, used in robotics.
-
DC Motors – Rotate continuously, used in fans or wheels.
-
Stepper Motors – Move in steps, useful for CNC machines and 3D printers.
Audio Output Devices (Produce Sound)
-
Buzzers – Create simple beeps or tones.
-
Speakers – Play music or voice sounds.
Electromechanical Output Devices (Control Other Components)
- Relays – Switch high-power devices on/off using a small signal.
Choosing the input device¶
The goal of this assignment is to integrate an output device with a microcontroller board and program it to perform a specific function. For this task, I have chosen to use a servo motor as the output device.
Understanding the mechanism¶
A servo motor is a type of motor that moves to a specific angle instead of rotating continuously like a regular motor. It is controlled by sending PWM (Pulse Width Modulation) signals from a microcontroller.
It works using a small motor, gears, and a position sensor. When the microcontroller sends a signal, the motor adjusts its position accordingly and holds it steady. This makes servo motors ideal for tasks that need precise movement, like robotics, automation, and control systems.
Connections in Servo Motor¶
The sensor has three main pins:
- Power (VCC/Red Wire)
- Ground (GND/Black or Brown Wire)
- Control Signal (PWM/Yellow or Orange Wire)
Types of Servo Motors¶
Based on the application servo motors are classified into different types:
-
AC Servo Motor – Operates using AC power, used in industrial applications.
-
DC Servo Motor – Operates using DC power, commonly used in small robotic systems.
-
Brushless DC Servo Motor – Offers high efficiency and durability, used in high-performance applications.
-
Positional Rotation Servo Motor – Moves within a fixed range (0° to 180°), used for precise positioning.
-
Continuous Rotation Servo Motor – Rotates in either direction without a fixed stop, used in wheels and conveyor systems.
-
Linear Servo Motor – Converts rotational motion into linear movement, useful for actuators and sliding systems.
Individual Assignment¶
Servo Motor¶
To do this I will be using the breakout board that i made during week 8
Before writing any code, I first connected the servo motor to the XIAO RP2040.
Wire coneection
Servo Motor Wire | XIAO RP2040 Pin | Description |
---|---|---|
VCC (Red) | 3V3 (for small servos) or 5V (external power) | Power supply for the servo |
GND (Black/Brown) | GND (Pin 12) | Ground connection |
Signal (Yellow/Orange/White) | D2 (GP2, Pin 2) | PWM signal pin (control line) |
Programming the Servo Motor
I tried understanding how to write the code using chatgpt and the write my own code.
Prompt: Can you explain how servo motors work and how to write code from scratch for controlling them? Specifically, I want to understand the concepts of PWM
I started by including the Servo library #include <Servo.h>
in the code to easily control the servo motor.
Then, you create a servo object that will represent the servo in your code: Servo myServo and then you attach the servo object to the pin that the control wire of the servo is connected to using myServo.attach(A0); given that we are connecting the control YELLOW wire to A0.
After this you put simple commands to move the servo with myServo.write(90); which will move the motor to 90 degree or whatever angke you mention.
First Attempt: Controlling the Servo with Fixed Angles¶
I started with a simple program to move the servo to 0°, 90°, and 180° with a delay between each movement.
I began by writing a simple program to control the servo motor at specific angles.
The idea was to move the servo to 0°, 90°, and 180° with a delay in between. This was meant to verify that the servo was responding correctly to commands
Code
#include <Servo.h>
Servo myServo; // Create servo object
int servoPin = D2; // Use GP2 (D2 on XIAO RP2040)
void setup() {
myServo.attach(servoPin); // Attach the servo to GP2
}
void loop() {
myServo.write(0); // Move to 0 degrees
delay(1000);
myServo.write(90); // Move to 90 degrees
delay(1000);
myServo.write(180); // Move to 180 degrees
delay(1000);
}
Issues encountered:
- In Arduino IDE the correct port was not automatically selected. To fix this I manually selected the port from Tools > Port.
- Initially, the code generated by ChatGPT didn’t work as expected. After some debugging, I realized that it was missing an essential component—the Servo library. I had to specifically prompt ChatGPT to include the SERVO LIBRARY to the code. This experience taught me that while ChatGPT can be a helpful tool, it's important to critically review and understand the code it provides rather than relying on it blindly.
Second Attempt: Controlling the Servo with Fixed Angles¶
To achieve smoother motion, I modified the code so that the servo would move gradually from 0° to 180° and back, instead of jumping directly between positions.
This was done using a for loop to change the servo position incrementally
Code
#include <Servo.h>
Servo myServo;
int servoPin = D2; // Use GP2 (D2 on XIAO RP2040)
void setup() {
myServo.attach(servoPin);
}
void loop() {
for (int pos = 0; pos <= 180; pos += 10) { // Move from 0° to 180°
myServo.write(pos);
delay(50);
}
for (int pos = 180; pos >= 0; pos -= 10) { // Move from 180° back to 0°
myServo.write(pos);
delay(50);
}
}
Solenoid¶
I also tried experimenting with solenoid. A solenoid is an electromechanical device that converts electrical energy into linear motion. When an electric current passes through the coil, it creates a magnetic field, which moves a metal plunger inside the coil.
To understand solenoid mechanisms better, I referred to an online guide on making solenoid coils Instructables link. This helped me learn how the coil is wound and how solenoids function practically
I started off by taking a plastic tube. For which i used a pen and cut of its edges.
To support the sides of the pen. I made a donut shaped laser cut out.
Then I took an enamel-coated copper wire and wrapped it around a plastic tube. Leave the two wire ends ends exposed for electrical connections.
Ensured the wire is wound tightly and neatly to maximize the efficiency of the electromagnetic field.
Then I scrapped off the enamel coating at the wire ends using a blade to ensure proper connectivity.
I used a DC Voltage motor and set its value to 12V.
Then I connected the two ends of the wire to the pins
I inserted an nail inside the coil to enhance the magnetic effect. Observed the nail for movement, expecting it to get pulled inside the coil due to the magnetic field.
Learnings
Even though the solenoid did not work as expected, I gained important insights into the challenges of working with electromagnets and solenoids. Key takeaways:
- The number of coil turns and power source selection are crucial for proper functioning.
- The length-to-diameter ratio of the solenoid impacts efficiency—shorter, thicker coils often work better
- Magnetic field strength is directly proportional to the current and the number of turns, so optimizing these factors is key.
4 Digit Display¶
The TM1637 module allows control of a 4-digit 7-segment display with just two digital pins from a microcontroller.
Connections
TM1637 Pin | XIAO RP2040 Pin | Description |
---|---|---|
CLK | D2 (GP2) | Clock Pin |
DIO | D3 (GP3) | Data I/O Pin |
VCC | 3.3V / 5V | Power Supply |
GND | GND | Ground |
Making a counting display
I wanted to make a simple counting display using the TM1637 4-digit 7-segment display. I first displayed the number 2025, and then made the display count from 0 to 99 with a small delay in between. This helped me understand how to control the display and send numbers
Upload the code
Code
#include <TM1637Display.h>
#define CLK D2 // connected to TM1637 CLK
#define DIO D3 // connected to TM1637 DIO
TM1637Display display(CLK, DIO);
void setup() {
display.setBrightness(5); // brightness: 0 (dim) to 7 (bright)
}
void loop() {
// Display a number
display.showNumberDec(2025); // Displays: 2025
delay(2000);
// Count up
for (int i = 0; i < 100; i++) {
display.showNumberDec(i);
delay(100);
}
display.clear();
delay(1000);
}
Error: After uploading the code I got an error as TM1637Display library was not installed.
To fix it you need to install the TM1637Display library in the Arduino IDE.
To do this Go to Sketch > Include Library > Manage Libraries. Serach for TM1637Displa by Avishay Orpaz and install it.
Final working
Group Work¶
We checked the power consumption of the output device (4 Digit Display). For this we frist used a Multimeter. We measured the the potential diffreence (P) and the flow of the current (I) when conencted to an output device i.e P=VI
I used the same connection and code which i ranned earlier. Once this was done.
Now make sure to connect the red probe marked V plug and the black probe to the COM common ground. As we are measuring the Dc voltage turn the dial to V as shown in the image below.
For reading my output device I touched the red probe (positive) with the 5V pin and the black probe with the ground (negative) pin of Xiao RO2040. This configuration allowed me to measure the voltage and current flowing through the 4-digit display when it was operating.
Next, I pressed the Min/Max button on the multimeter. This helped me see the minimum and maximum current the 4-digit display used while it was running.
This shows how the current changes depending on how many segments of the display are ON.
Power Calculation
V = 5 V
Exercise files¶
Below are the files for: