Exercise One: State Change Detector (Button Counter)
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - N.O. Button
1 - Jumper Wire Set
State change detection is a technique used in Arduino programming to identify when a digital input (like a button press) changes from one state to another. In this lesson, we'll learn how to implement state change detection using a button. We will do that by having the Arduino keep track of how many times we click the button.
Steps
Set Up the Circuit: Begin by connecting the button to the Arduino:
Connect one leg of the button to digital pin 2 on the Arduino.
Connect the other leg of the button to GND (ground) on the Arduino, using a resistor (pull-down resistor).
2. Write the Arduino Sketch
This code will require the use of two variable blocks, math blocks, the print to serial monitor block, an IF logic statement, and both the "On Start" and "Forever" loop command blocks.
int Click = 0;
int Counter = 0;
void setup()
{
pinMode(2, INPUT);
Serial.begin(9600);
Click = 0;
Counter = 0;
}
void loop()
{
Click = digitalRead(2);
if (Click == HIGH) {
Counter = (Counter + Click);
Serial.print("Number of Clicks: ");
Serial.println(Counter);
delay(150); // Wait for 150 millisecond(s)
}
}
By opening and printing to the Serial Monitor, the Arduino and IDE is able to show you how many clicks the button has received by counting the number of state changes the button has had between 0 and 1, or HIGH and LOW. Because this state change was assigned to a variable, the variable keeps track of the number of times clicked and adds one each time there is another click.
3. Understand the Code
int Click = LOW (0); stores the current state of the button.
int Counter = LOW (0); stores the previous state of the button.
In the loop():
Click = digitalRead(2); reads the current state of the button.
The wait block checks if enough time has passed for debouncing.
If so, it checks for a change in button state and prints a message if the button is pressed.
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor). You will now see a continuous stream of digital readings (0 or 1) indicating the state of the button.
5. Experiment and Observe
Press and release the button. Observe how the Arduino keeps track of how many times your button has been pressed by triggering each time the state of the button changes.
This lesson provides a foundational understanding of how to use state change detection, an important technique for projects where you need to respond to specific events triggered by digital inputs.
Questions:
Can you use variables to store and update the count value based on button clicks?
How could you expand the project to reset the count or perform other actions based on button clicks?
Can you think of applications where counting interactions or events is valuable?
What are common issues that might arise when working with digital counters and button clicks?
Exercise Two: RBG LED Rainbow
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 1/4 Watt 470 Ohm THR
1 - RBG LED
1 - Jumper Wire Set
In this project, we'll use an RGB (Red, Green, Blue) LED to cycle through colors of the rainbow. This involves smoothly transitioning between different combinations of red, green, and blue to create a dynamic, colorful effect.
Steps
Set Up the Circuit: Begin by connecting the RGB LED to the Arduino:
Connect the red leg of the LED to the digital pin ~3 on the Arduino.
Connect the green leg of the LED to the digital pin ~5 on the Arduino.
Connect the blue leg of the LED to the digital pin ~6 on the Arduino.
Connect the cathode of the LED to a 470-ohm resistor to ground.
2. Write the Arduino Sketch
This code will require the "Forever" loop command block, the wait command block, and the RGB output block. In the example below, only two of the colors are shown in the loop. To add colors, simply duplicate the RGB LED output block and the wait command block. In this exercise, your goal should be to make the colors of the rainbow: Red, Orange, Yellow, Green, Blue, Indigo, and Violet.
void setup()
{
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
analogWrite(3, 255);
analogWrite(5, 0);
analogWrite(6, 0);
delay(1000); // Wait for 1000 millisecond(s)
analogWrite(3, 255);
analogWrite(5, 153);
analogWrite(6, 0);
delay(1000); // Wait for 1000 millisecond(s)
}
3. Understand the Code
pinMode(redPin, OUTPUT);, pinMode(greenPin, OUTPUT);, and pinMode(bluePin, OUTPUT); set the corresponding pins as outputs.
In the loop():.
analogWrite(OUTPUT, PWM) writes the red, green, and blue values (0-255) and sets the corresponding LED pins using analogWrite().
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
5. Experiment and Observe
Press and release the button. Observe how the Arduino keeps track of how many times your button has been pressed by triggering each time the state of the button changes.
This lesson provides a foundational understanding of how to use state change detection, an important technique for projects where you need to respond to specific events triggered by digital inputs.
Questions:
How does the RGB LED contribute to creating a cycling color effect?
How do you control the RGB LED in an Arduino sketch to cycle through colors?
Can you explain how changing the intensity of the red, green, and blue channels creates different colors?
How could you integrate user input, such as a potentiometer, to dynamically control the color cycling speed or pattern?
Exercise Three: RBG LED Controller
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - RBG LED
3 - 100K Potentiometer
1 - Jumper Wire Set
Creating a basic setup where a potentiometer controls an RGB LED on Arduino involves connecting the components and writing a simple sketch. This project showcases the fundamental principle of using analog input to control output intensity. Here's a step-by-step guide:
Steps
Set Up the Circuit:
Begin by connecting the RGB LED to the Arduino:
Connect the red leg of the LED to the digital pin ~3 on the Arduino.
Connect the green leg of the LED to the digital pin ~5 on the Arduino.
Connect the blue leg of the LED to the digital pin ~6 on the Arduino.
Connect the cathode of the LED to a 470-ohm resistor to ground.
Connect the three (3) Potentiometers:
Connect one end of the potentiometer to the 5V pin on the Arduino.
Connect the other end of the potentiometer to the GND (ground) pin.
Connect the middle pin (wiper) of the potentiometer to analog pin A0-A2 on the Arduino.
2. Write the Arduino Sketch
This code will require the "Forever" loop command block, variables, and digital PWM output blocks. Each potentiometer can only control one leg and therefore color of the RGB LED. This means you must map them to variables three different times. If you are unsure how to do this, check out our Analog Mapping exercise on our Analog Arduino Projects Page
int Red = 0;
int Green = 0;
int Blue = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
Red = map(analogRead(A0), 0, 1023, 0, 255);
Green = map(analogRead(A1), 0, 1023, 0, 255);
Blue = map(analogRead(A2), 0, 1023, 0, 255);
analogWrite(3, Red);
analogWrite(5, Green);
analogWrite(6, Blue);
delay(10); // Delay a little bit to improve simulation performance
}
3. Understand the Code
int Red = 0; int Green = 0; int Blue = 0; Setting up variables for Red, Green, and Blue and starting their values at zero.
pinMode(3, OUTPUT);, pinMode(5, OUTPUT);, and pinMode(6, OUTPUT); set the corresponding PWM pins as outputs.
pinMode(A0, INPUT);, pinMode(A1, INPUT);, and pinMode(A2, INPUT); set the corresponding pins analog inputs.
In the loop():.
Red = map(analogRead(A0), 0, 1023, 0, 255); Mapping the corresponding color variable to the min/max of PWM of the LED.
analogWrite(OUTPUT, PWM) writes the red, green, and blue values (0-255) and sets the corresponding LED pins using analogWrite().
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
5. Experiment and Observe
Press and release the button. Observe how the colors change through different blends of Red, Green, and Blue as their intensities change.
This setup demonstrates the core principle of using analog input (from the potentiometer) to control an output (the RGB LED). It serves as a foundation for more complex projects involving analog sensors and digital outputs. Understanding this basic concept is crucial for creating dynamic and responsive electronic systems.
Questions:
How do the three potentiometers contribute to controlling the RGB values of the LED?
What functions or techniques are used to map potentiometer values to RGB color channels?
Can you explain how changing the potentiometer positions affects the color of the RGB LED?
Can you think of applications where users can customize and interact with the lighting environment?
Exercise Four: Servo Sweep
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 180 Servo
1 - Jumper Wire Set
In this project, we'll make a servo motor sweep back and forth across its range of motion. This is a great way to understand how to control a servo motor with an Arduino.
Steps
Set Up the Circuit:
Connect the power wire (usually red or orange) of the servo motor to the 5V pin on the Arduino.
Connect the ground wire (usually brown) of the servo motor to any GND pin on the Arduino.
Connect the signal wire (usually yellow) of the servo motor to digital pin 9 on the Arduino.
2. Write the Arduino Sketch
This code will require the "Forever" loop command block, the count command block, variables, and digital PWM output blocks.
#include <Servo.h>
int pos = 0;
Servo servo_9;
void setup()
{
servo_9.attach(9, 500, 2500);
}
void loop()
{
for (pos = 0; pos <= 180; pos += 1) {
servo_9.write(pos);
delay(15); // Wait for 15 millisecond(s)
}
for (pos = 180; pos >= 0; pos -= 1) {
servo_9.write(pos);
delay(15); // Wait for 15 millisecond(s)
}
}
3. Understand the Code
#include <Servo.h> includes the Servo library, which provides easy control of servo motors.
Servo servo; creates an instance of the Servo class called servo.
int pos = 0; initialize variables for controlling the servo's angle.
servo_9.attach(9); attaches the servo motor to pin 9.
In the loop():
The first for loop sweeps the servo from 0 to 180 degrees.
The second for loop sweeps the servo from 180 to 0 degrees.
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
5. Experiment and Observe
Press and release the button. Observe how the Servo Sweeps from 0 degrees to 180 degrees and back.
This project demonstrates how to control a servo motor with an Arduino. Servo motors are commonly used in various applications, such as robotics and automation. Understanding how to control them opens up a wide range of possibilities for creating moving and interactive projects.
Extra Experiment:
Using the Oscilloscope tool on TinkerCAD, attach positive to the signal pin of the servo and negative to ground. Set the Oscilloscope to 1m/s divisions and simulate. You will see the pulse width modulation change as the servo sweeps. This is the signal telling the servo what position to spin to- the width of this signal is set to degrees in the servo's internal control unit.
Questions:
How is the servo motor connected to the Arduino board?
What functions or techniques are used to define the range and speed of the sweep?
Can you explain how changing parameters in the sketch affect the speed and range of the servo's motion?
How could you modify the project to allow interactive control, perhaps using a potentiometer or other input device?
Can you add features like pausing, changing speed dynamically, or responding to external triggers?
Exercise Five: Servo Controller
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 180 Servo
1 - 100K Potentiometer
1 - Jumper Wire Set
In this project, we'll use a potentiometer to control the position of a servo motor. This allows you to manually set the angle of the servo using the potentiometer knob.
Steps
Set Up the Circuit:
Connect the power wire (usually red or orange) of the servo motor to the 5V pin on the Arduino.
Connect the ground wire (usually brown) of the servo motor to any GND pin on the Arduino.
Connect the signal wire (usually yellow) of the servo motor to digital pin 9 on the Arduino.
Connect one end of the potentiometer to the 5V pin on the Arduino.
Connect the other end of the potentiometer to the GND (ground) pin.
Connect the middle pin (wiper) of the potentiometer to analog pin A0 on the Arduino.
2. Write the Arduino Sketch
This code will require the "Forever" loop command block, variables, mapping, and digital PWM output blocks. Each potentiometer can only control one leg and therefore color of the RGB LED. This means you must map them to variables three different times. If you are unsure how to do this, check out our Analog Mapping exercise on our Analog Arduino Projects Page
#include <Servo.h>
int pos = 0;
Servo servo_9;
void setup()
{
pinMode(A0, INPUT);
servo_9.attach(9, 500, 2500);
}
void loop()
{
pos = map(analogRead(A0), 0, 1023, 0, 180);
servo_9.write(pos);
delay(10); // Delay a little bit to improve simulation performance
}
3. Understand the Code
#include <Servo.h> includes the Servo library, which provides easy control of servo motors.
Servo servo; creates an instance of the Servo class called servo.
int pos = 0; initialize variables for controlling the servo's angle.
servo_9.attach(9); attaches the servo motor to pin 9.
In the loop():
pos = map(analogRead(A0), 0, 1023, 0, 180); this line maps the potentiometer from 0 to 180 degrees to a variable called "pos" for position.
servo_9.write(pos); Sets position to the angle of the potentiometer mapped to 180 degrees.
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
5. Experiment and Observe
Press and release the button. Observe how the Servo Sweeps from 0 degrees to 180 degrees and back.
This project demonstrates how to use a potentiometer as an input device to control the position of a servo motor. It's a useful skill for creating projects that require precise control over the position of a motor, such as robotic arms and other automated systems.
Questions:
How does the potentiometer contribute to controlling the servo motor's position?
What functions or techniques are used to map potentiometer values to the servo motor's position?
How does the potentiometer enable interactive control over the servo motor's position?
Can you think of applications where manual control over a mechanical system is valuable?
What concepts does this project help you understand regarding analog sensors, variable mapping, and servo motor control?
How does this project build on your knowledge of basic Arduino programming and motor control?
Exercise Six: Proximity Sensor
Background Knowledge:
How to read a Schematic
How to use a Breadboard
Proximity Sensor
Introduction to Arduino with TinkerCAD
Digital Sensors
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - Ultra Sonic Proximity Sensor
1 - Piezo Buzzer
1 - Jumper Wire Set
In this project, we'll create a simple alarm system using a proximity sensor and a buzzer. The buzzer will sound when an object is detected within a certain range of the proximity sensor.
Steps
Set Up the Circuit:
Connect the VCC pin of the proximity sensor to 5V on the Arduino.
Connect the GND pin of the proximity sensor to GND on the Arduino.
Connect the TRIG pin of the proximity sensor to digital pin 3 on the Arduino.
Connect the ECHO pin of the proximity sensor to digital pin 2 on the Arduino.
Connect one leg of the buzzer to digital pin 8 on the Arduino.
Connect the other leg of the buzzer to GND through a resistor (around 220 ohms).
2. Write the Arduino Sketch
This code will require the "Forever" loop command block, variables, IF Logic statements, Math commands, a unique input command for the ultrasonic sensor, and a unique command for outputting a tone. There are two types of Ultrasonic sensors: one has a four-pin configuration where the trigger and echo pin are separate and the other has a three-pin configuration where they are combined. This command will be different depending on the hardware you are using. If you need more details on the Ultrasonic sensor, check out the section on them under the Analog Sensor page.
Arduino itself doesn't inherently measure in either imperial or metric units. Instead, the measurement units are determined by the sensors or components you use in your Arduino projects. For example:
Distance Sensors: If you are using a distance sensor like an ultrasonic sensor (HC-SR04), it typically provides distance measurements in centimeters. You can convert these measurements to inches or other units if needed.
int Inches = 0;
int Centimeters = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
pinMode(8, OUTPUT);
}
void loop()
{
if (0.006783 * readUltrasonicDistance(3, 2) < 20) {
tone(8, 523, 100); // play tone 60 (C5 = 523 Hz)
}
delay(10); // Delay a little bit to improve simulation performance
}
3. Understand the Code:
The code uses a simple distance calculation based on the time it takes for the ultrasonic pulse to return. If the calculated distance is less than 20 inches (adjust as needed), the buzzer is activated; otherwise, it's deactivated.
0.006783 * readUltrasonicDistance(3, 2); reads ultrasonic sensor with the trigger on pin 3 and echo on pin 2. .006783 is a multiplier to convert the ultrasonic sensor to inches.
(0.01723 * readUltrasonicDistance(3, 2); reads ultrasonic sensor with the trigger on pin 3 and echo on pin 2. .006783 is a multiplier to convert the ultrasonic sensor to centimeters.)
tone(8, 523, 100); sets the speaker to output on pin 8, plays a tone of 523 Hz which is C5, and plays for 100ms.
4. Upload and Monitor:
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
5. Test the Alarm:
Place an object within the proximity of the sensor. The buzzer should sound when the object is within the specified range.
Questions:
How does the proximity sensor work in detecting the presence of an object?
What role does the buzzer play in this alarm system?
How could you adjust the threshold distance to customize the alarm's sensitivity?
Can you think of real-world applications for a proximity sensor-based alarm system?
What challenges might arise in using this system, and how could they be addressed?
How does this project contribute to your understanding of sensor-based systems and their applications?
Exercise Seven: Motor Controller
Background Knowledge:
How to read a Schematic
How to use a Breadboard
Resistors, Transistors, and Diodes
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 6-12V DC Motor
1 - 1K 1/4 Watt THR
2 - 10K 1/4 Watt THR
2 - 3904 NPN Transistor
2 - 3906 PNP Transistor
1 - 100K Potentiometer
1 - Jumper Wire Set
Step into the world of precise motor control with the Arduino Motor Controller Circuit – a gateway to unleashing the full potential of your electric motors. This innovative circuit, built upon the versatile Arduino platform, empowers enthusiasts and engineers to regulate the speed and direction of motors with unparalleled ease. By seamlessly integrating sensors, feedback mechanisms, and programmable logic, the Arduino Motor Controller Circuit opens up a realm of possibilities for automation, robotics, and DIY projects. Whether you're a hobbyist or a seasoned developer, this circuit provides a user-friendly interface to harness the dynamic capabilities of electric motors, transforming your ideas into seamless, controlled motion
Motor Set-Up One
Steps
Set Up the Circuit:
Connect the battery to the Arduino Vin and ground
Connect the motor to a PWM pin and ground.
2. Write the Arduino Sketch
The speed regulation of a DC motor is intricately linked to the voltage it receives. In order to optimize a motor's performance and ensure it operates at its maximum power, maintaining high levels of both voltage and current is crucial. The Arduino employs a technique known as pulse width modulation (PWM) to achieve precise control over a motor's speed. Instead of directly adjusting the voltage supplied to the motor, PWM rapidly switches the motor between full power and no power. By modulating the duration of the 'on' and 'off' states in a cycle, the Arduino effectively controls the average power delivered to the motor. When the motor is on for a greater portion of the cycle, it spins faster, and when it's off for a longer duration, the speed decreases. This dynamic manipulation of the power ratio enables the Arduino to finely adjust the speed of the motor without compromising its overall power output.
When writing this sketch, just output a PWM pin to between 0-255.
3. Understand the Code:
analogWrite(3, 0); analogWrite(3, 255);
Set the motor to 0% or 100% power.
4. Upload and Monitor:
Upload the sketch to the Arduino board.
5. Test the Motor Controller:
The motor should spin at a speed in proportion to the 0-225 PWM number you use in your code.
Motor Set-Up Two
While it is possible to control a DC motor directly from an Arduino, there are certain scenarios where doing so might not be the best approach. Here are some reasons why you might choose not to control a DC motor directly from an Arduino:
Current and Voltage Requirements: DC motors often require higher current and voltage than what an Arduino can directly provide. Attempting to drive a motor that exceeds the Arduino's output capabilities can damage the Arduino or lead to unreliable motor performance.
Back Electromotive Force (EMF): DC motors generate back electromotive force (EMF) when they spin, which can create voltage spikes. Handling these spikes properly, using components like diodes, is more complex when driving a motor directly from an Arduino.
Motor Noise and Interference: Motors can introduce electrical noise and interference into the circuit. This might affect the stability and performance of other components in the Arduino circuit, especially in sensitive applications.
Limited PWM Pins: Arduino boards have a limited number of PWM (Pulse Width Modulation) pins, which are essential for smooth motor speed control. If you need to control multiple motors independently or need more PWM channels, additional hardware like motor driver modules might be necessary.
Overloading the Arduino: Driving a motor directly from an Arduino can place a heavy load on the microcontroller, limiting its ability to perform other tasks concurrently. This can lead to reduced overall system performance.
Temperature Considerations: DC motors can generate heat during operation, and this heat can affect the Arduino if they are in close proximity. Proper thermal management might be required.
For these reasons, it's often recommended to use external motor driver modules or controllers designed for the specific requirements of DC motors. These modules can handle higher currents, provide proper isolation, and include features like built-in protection circuits, making them more suitable for motor control applications.
Steps
Set Up the Circuit:
Connect the battery to the Arduino Vin and ground
Connect the motor in series with an NPN transistor. This should be connected straight to the positive and negative of the battery.
Connect a PWM pin to the base of the NPN transistor
Connect a non-polarized capacitor and diode in a reverse-bias configuration between the negative and positive of the motor.
Connect the potentiometer to 5V, A0, and ground. This will act as our sensor to control the speed of the motor.
Why a Capacitor?
Using a capacitor in parallel with a DC motor is a common practice in some applications, but it's not a strict requirement for all setups. The decision to include a capacitor depends on the specific characteristics of the motor, the power supply, and the requirements of the circuit. Here are a few considerations for using a capacitor in parallel with a DC motor:
Noise Reduction: Capacitors can help reduce electrical noise generated by the motor during operation. This is particularly important in sensitive electronic circuits where interference could affect performance.
Stabilizing Voltage: Capacitors can act as a buffer to stabilize the voltage supplied to the motor, especially in situations where there may be fluctuations or spikes in the power supply.
Reducing Sparking: For brushed DC motors, which have brushes that make and break connections as the motor spins, a capacitor in parallel can help suppress sparking at the brushes, potentially extending the lifespan of the motor.
Improving Motor Performance: In some cases, adding a capacitor can help improve the overall performance of the motor, especially when there are issues with sudden changes in load or speed.
However, not all DC motor setups require a capacitor, and in some cases, it might not be necessary or could even be detrimental. It's essential to consider the specific characteristics of your motor, the power supply, and the requirements of your application before deciding to include a capacitor. If in doubt, referring to the motor manufacturer's recommendations or consulting relevant documentation is advisable.
Why a Diode?
Placing a diode in parallel with a DC motor, commonly referred to as a "freewheeling" or "flyback" diode, is a common practice in electronic circuits. The primary purpose of this diode is to provide a path for the inductive energy stored in the motor's coil to dissipate safely when the power to the motor is suddenly cut off. When a DC motor is in operation, it builds up magnetic energy in its coil. If the power to the motor is suddenly switched off, this energy needs a path to dissipate. Without a freewheeling diode, the collapsing magnetic field in the motor coil can create a voltage spike, known as a back electromotive force (EMF), that can potentially damage other components in the circuit.
The freewheeling diode provides a low-resistance path for the inductive energy to circulate back into the circuit when the power is turned off. By doing so, it prevents voltage spikes and protects other components from potential damage. The diode is typically connected in reverse bias, meaning its cathode is connected to the positive side of the motor and its anode to the negative side. This configuration ensures that when the power is turned off, the diode becomes forward-biased, allowing the inductive energy to circulate through the diode and back into the circuit.
In summary, the freewheeling diode is a protective measure to prevent voltage spikes and potential damage to the circuit when the power to a DC motor is abruptly interrupted.
2. Write the Arduino Sketch
Since there are so many downsides to directly connecting a motor to an Arduino, we can use a transistor to get the power from the power source and not the Arduino itself. Utilizing linear flow control inherent in transistors, we can regulate the total power supplied to the motor, thereby controlling its speed through adjustment of the PWM signal directed to the transistor's base. A PWM signal of 255 corresponds to 100% power, while a signal of 0 results in 0% power. Ensure your code aligns with the physical operations of the circuit as you develop your script. Your code should also include mapping your potentiometer to the PWM range for the motor.
3. Understand the Code:
analogWrite(3, map(analogRead(A0), 0, 1023, 0, 255));
Maps the range of the potentiometer, and analog sensor, to the motor 0% or 100% power.
4. Upload and Monitor:
Upload the sketch to the Arduino board.
5. Test the Motor Controller:
Adjust the potentiometer to control the speed of the motor. You can control this motor in confidence knowing you won't overcurrent or damage any of the components.
Motor Set-Up Three
The final set-up that we will cover is using an H-bridge. An H-bridge is a circuit configuration commonly used for controlling the direction and speed of DC motors. It consists of four switches arranged in the shape of an "H," hence the name. The H-bridge allows the motor to be driven in both forward and reverse directions and enables speed control through Pulse Width Modulation (PWM). Here's an explanation of how an H-bridge works to control motors:
Components of an H-Bridge:
Four Switches (Transistors or MOSFETs): The H-bridge has four switches, typically labeled as Q1, Q2, Q3, and Q4. Each pair of switches (Q1/Q2 and Q3/Q4) represents one side of the H.
DC Motor: The motor is connected between the outputs of the two sides of the H-bridge. The direction of the current through the motor determines its rotation direction.
Power Supply: The H-bridge requires a power supply to drive the motor. The voltage of the power supply should match the motor's specifications.
Operation:
Forward Rotation: To make the motor rotate in the forward direction, switches Q1 and Q4 are turned on, while Q2 and Q3 are turned off. This creates a complete circuit, allowing current to flow from the power supply, through the motor, and back to the power supply. The motor spins in the desired direction.
Reverse Rotation: To reverse the motor's rotation, switches Q2 and Q3 are turned on, while Q1 and Q4 are turned off. This reverses the direction of the current flow through the motor, causing it to rotate in the opposite direction.
Braking: By turning off all switches, the motor is effectively disconnected from the power supply. However, the back electromotive force (EMF) generated by the motor could damage the circuit. To address this, a technique called "regenerative braking" involves turning on both switches on one side of the H-bridge to create a short circuit, allowing the motor to dissipate its energy safely.
Speed Control with PWM: To control the motor's speed, Pulse Width Modulation (PWM) is applied to the switches. By rapidly switching the switches on and off with varying duty cycles, the effective voltage applied to the motor is adjusted, regulating its speed.
Using an H-bridge provides a flexible and efficient way to control DC motors, making it a fundamental component in various applications such as robotics, automation, and electric vehicles.
Steps
Set Up the Circuit:
Connect the battery to the Arduino Vin and ground
Connect the motor in series with an NPN transistor. This should be connected straight to the positive and negative of the battery.
Connect a PWM pin to the base of the NPN transistor
Connect a non-polarized capacitor and diode in a reverse-bias configuration between the negative and positive of the motor.
Connect the potentiometer to 5V, A0, and ground. This will act as our sensor to control the speed of the motor.
2. Write the Arduino Sketch
This project employs a transistor H-bridge, enabling the manipulation of both motor speed and direction. Utilizing linear flow control inherent in transistors, we can regulate the total power supplied to the motor, thereby controlling its speed through adjustment of the PWM signal directed to the transistor's base. A PWM signal of 255 corresponds to 100% power, while a signal of 0 results in 0% power. Additionally, by selecting the side of the H-bridge receiving the PWM signal, we can govern the motor's direction. Ensure your code aligns with the physical operations of the circuit as you develop your script.
3. Understand the Code:
if (digitalRead(13) == HIGH) {
analogWrite(3, 0);
analogWrite(5, map(analogRead(A0), 0, 1023, 0, 255));
Since this motor controller has a forward and reverse you need to set up a logic condition that will switch the digital outputs between high and low. Your high will be mapped to your potentiometer and your low will be zero.
4. Upload and Monitor:
Upload the sketch to the Arduino board. If you want, you can add code that will map your motor speed to a percentage to be seen on the serial monitor. You can also set it to say forward and reverse. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
5. Test the Motor Controller:
Adjust the potentiometer to control the speed of the motor. You can control this motor in confidence knowing you won't overcurrent or damage any of the components.
Questions:
Why is it bad practice to control a motor directly from an Arduino?
How do you adjust the motor speed using Pulse Width Modulation (PWM) signals in your Arduino code?
What is the significance of linear flow control in transistors, and how is it utilized in the motor control process?
Can you explain the role of the transistor H-bridge in the motor control circuit and how it facilitates speed and direction adjustments?
Can you describe any safety measures implemented in the motor control system to prevent damage or malfunction?
How do you handle potential issues such as voltage spikes or back EMF in the motor control circuit?
Exercise Seven: LCD Display (Coming Soon)
Background Knowledge:
How to read a Schematic
How to use a Breadboard
Proximity Sensor
Introduction to Arduino with TinkerCAD
Digital Sensors
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 180 Servo
1 - 100K Potentiometer
1 - Jumper Wire Set