Arduino & Servo
Arduino & Servo: Moving an Arm
June 14, 2024
STEAM Clown ™ Productions
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Teachers: Before You Present
2
Click on "STEAM Clown's Taipei Workshops"
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Licensing & Attribution
Open Source Philosophy - I have come to realize that I’m really not competing with other teachers. Maybe I have some local competition, but in reality, if a student is not coming to my class or my school, then I’m not competing with other teachers in any way… If you work more than a District away, then we really are not competitors. Let's Share... Collaborate... Help each other... I, and you, may have sweat blood and tears developing a lesson plan, a lab, or presentation… There is an ENORMOUS unpaid value there… I get that… I don’t begrudge teachers who are trying to sell their collateral… I’m just saying that is not what I’m going to do. But that is my plan. I’m going to share virtually everything I develop. Join Me!!! See More
CC BY-NC-SA 4.0
https://creativecommons.org/licenses/by-nc-sa/4.0/
https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
GNU Public License & EUPL (European Union Public Licence)
Any included or linked Programming Code Is licensed under GNU General Public License v3.0 & and / or licensed under EUPL 1.2 or later
See the Appendix for Additional Licensing & Attribution Information
3
Please maintain this slide with any modifications you make:
This content is licensed as Open Source / Creative Commons: A Specific use license has been provided to any organizations listed in the Appendix
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Objective, Overview, & Introduction
Move A Robot Arm With A Servo?
4
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
What You Will Know...
Prior Knowledge & Certifications
What You Will Know & Be Able To Do
5
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
How You Will Be Measured
6
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Essential Questions
7
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Resources & Materials Needed
8
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
New Words or Concepts...
9
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Workshop Introduction
Connecting Hardware & Code To The Outside World… But First In A Simulator, Then IRL
10
Are you ready to take your learning into the future?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
👉 Try This:
This is a Hands On Project. Try Stuff!!!!
11
print("Hisssssssssss")
Try This 👉
Try This
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
TinkerCAD Account Setup
Connecting Hardware & Code In A Simulator
12
Are you ready to take your learning into the future?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
TinkerCAD - Join NOW
Click JOIN NOW or Sign In
13
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
TinkerCAD Introduction
Connecting Hardware & Code In A Simulator
14
Are you ready to take your learning into the future?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
TinkerCAD - Learn About Electronic Circuits
Click JOIN NOW or Sign In
15
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Arduino Introduction
Connecting Hardware & Code To The Outside World
16
Are you ready to take your learning into the future?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Did You Say “Arduino”?
17
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Go From a Sketch to Blinking an LED
Compiler�& Linker
1011001010
0110101101
1110100100
1010101100
0101100110
1011001010
1000100101
0101101001
1110101100
0011100110
1111001010
1100101101
1010110100
1110101100
1111100110
0011001010
1101111110
1010110100
1110101100
1001100110
Bootloader
1101001011
1110001001
1011101100
1110011101
1101110101
Arduino
Library
Files
Executable
Source Code
Host Computer
CPU
Ports
Blinking LED
USB
Download
Cable
Flash Program Memory
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
What Are We Going To Do?
19
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Switch (Input) → Arduino → LED (Output)
20
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
LAB #1�Review Of Arduino and Blinking LED
21
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Arduino Uno Controlling the World… or just an LED
22
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Click "Tinker This" To Make Your Own Copy
23
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Click on "CODE"
24
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab#1 - Blink An LED
Run this code
25
// This is where you define pin directions
int LED = 13;
// The setup routine runs once when you load a sketch or press reset:
void setup()
{
// initialize the pins used in this sketch
pinMode(LED, OUTPUT); // LED where you assigned it on pin 13
}
// this "main" loop routine runs over and over again forever:
void loop()
{
//Set pin 13 to HIGH. This provides 5v to the LED & turns it on
digitalWrite(LED, HIGH);
delay(1000); //Wait for a second
//Set pin 13 LOW and This turns the LED off
digitalWrite(LED, LOW);
delay(1000); //Wait for a second
}
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab#2 - Move LED Pin To Something Other Than Pin 13
26
int LED = 2;
Pro Tip: Don't use pins 0 or 1. Why?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
LAB#3 - Add A Switch to Your Arduino and Blinking LED
27
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab#3
Connect A Switch & Read It's State
28
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab #3 - Read A Switch Value
To find out if a button is being pushed, what do we need to do?
29
int sensor_pin = 7; //assign button to pin 7
pinMode(sensor_pin, INPUT);
Pin 7 is now an Input
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Where Do We Connect The Switch Input?
30
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab #4 - Now that we know the pin the sensor is assigned, how do we store the value?
31
int sensor_state = 0;
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab #4 - How do we actually tell the Arduino to read an Input pin?
32
// Reads the Switch
sensor_state = digitalRead(sensor_pin);
Serial.print(sensor_state);
What else do we need to add to our code to be able to print to the consol?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab #4 - Setup a Arduino Sketch to be able to print?
33
// Enables Serial.print
Serial.begin(9600);
Serial.println(sensor_state);
Maybe add a delay(200); to make the print of the sensor_state slow down so you can see it…
Add this line when you want to print something
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab: Switch, Read It, Save It, Do Something…
Finish the lab where you have done the following:
34
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Turn on the LED when the Button is pushed
OK, Our LED is blinking on and off? Why?
35
void loop()
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
digitalWrite(LED,sensor_state);
In the digitalWrite() what can �we change to send the value of the switch?
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Switch (Input) → Arduino → LED (Output)
Show diagram of Sensor (Input) → Arduino → Motor (Output)
36
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Dig Deeper
Extra Credit Labs
37
Write code that uses an IF statement to check the sensor_state value and then do something based on that.
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Sensor (Input) → Arduino
38
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Arduino → Electro-Mechanical Device (Output)
39
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Servo Motor Introduction
Moving Things With Arduino C++ Programs
40
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Servo Motor Introduction
41
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Two Types of Servos
42
They Look The Same...
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Servo Horns - Moving Stuff
43
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Servo Horn Connection �Examples
44
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Pulse Width Modulation
PWM
45
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Averaging The "ON" Time
Remember the LED and Potentiometer Lab?
46
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Servo Motors Explained
Lets Watch a Servo Motor Video(6 min)
47
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
48
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
How a differential comparator works
49
=
+
R3
R3 ≠ R4 so, V3 ≠ V4
R2
R4
R1
R1 = R2 so, V1 = V2
If Vin changes up or
down so does V1 and V2
V1
V2
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
The PWM ON/OFF Time is Avaged to a Voltage
The Pulse Cycles is 20ms
50
VMotor POS
VPWM
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Actual PWM Voltage Comparator
51
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Let's Move Stuff...
PWM and Servo Labs
52
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Arduino Servo PWM Code
Import these Libraries. Add these lines of code to the top of your program. The Servo.h library keeps track of all the PWM timing
53
#include <Servo.h> // Import Servo Library
// create servo object to control a servo
// my_servo can be any name.
// Twelve servo objects can be on most Arduino boards
Servo my_servo;
int pos = 0; // variable to store the servo position
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Arduino Servo PWM Code
Setup the GPIO and Servo settings
54
my_servo.attach(9); // Servo on pin 9 to servo object
pos = 0; // Set position Variable
my_servo.write(pos); // Set the default start position
delay(1000); // Delay just a bit to get into position
In void setup()
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Tell Your Servo Where To Start
55
pos = 42; // Set position Variable
my_robot_servo.write(pos); // Set the start position
delay(1000);
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Telling The Servo What Position (Angle) To Go To
A Servo methods in the Servo.h library is my_servo.write(0)�
56
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Arduino Servo PWM Code
57
my_servo.write(0);
delay(3000);
my_servo.write(120);
delay(3000);
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Lab Time
58
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Hookup the Servo circuit
59
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Hookup the Servo circuit & Add Sensor or Button Input
60
等等,不要点击
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
61
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Team Building & Negotiation Skills
62
STEAM Clown ™️ Production
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
What Materials Can You Use?
63
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Design Thinking & Planning
What Will You Build?
64
10 Minutes
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
How To Launch?
65
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Ready?
Go Build!!!!!!
准备好?去建造!
Zhǔnbèi hǎo? Qù jiànzào!
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions
Thank You…
Questions?
67
Jim Burnham (AKA STEAM Clown)
© Copyright - STEAM Clown TM
Creative Commons Licenses - BY-NC-SA 4.0
STEAM Clown TM Productions