1 of 67

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

2 of 67

Teachers: Before You Present

  • Update the Canvas Links, or Delete the bullets linking to Canvas links (Typically Labs)

2

www.STEAMClown.org

Science

Technology

Engineering

Art

Math

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

3 of 67

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

4 of 67

Objective, Overview, & Introduction

Move A Robot Arm With A Servo?

  • You will, as a team, build an Arm Waving Robot
  • You will incorporate Design Thinking during your Kinetic Machine planning phase

4

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

5 of 67

What You Will Know...

Prior Knowledge & Certifications

  • You should have some working practice with a Microcontroller (Arduino) interfacing to the "Real World"

What You Will Know & Be Able To Do

  • You will be able to clearly explain the design process to use a Servo and Microcontroller
  • You will be able to create a block diagram of your Kinetic Machine incorporating sensors, a Microcontroller, Servo in your Kinetic system
  • Work as a Team

5

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

6 of 67

How You Will Be Measured

  • Show examples of Design Planning Drawings
  • Show Your Servo Waving an Arm, and Controlling it from an Arduino, with Code you designed, edited, and deployed

6

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

7 of 67

Essential Questions

  • How can inputs from the "Real World" be processed in a program, and then used to affect the "Real World" in a meaningful way?
  • How can you apply Design Thinking and Creative Engineering to making something move?

7

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

8 of 67

Resources & Materials Needed

  • PC, Laptop or Other device to access sites & applications specified by your Instructor
  • A Microcontroller (an Arduino is used in this presentation)
  • Switches, Sensors, Motors, Servos and other electronic components
  • Makey Bot kit, or Crazy and Creative use of material like Duct Tape, Legos, Hot Glue, Cardboard, Pipe Cleaners, and other stuff to add "Bling" to your Arm Waving Robot…

8

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

9 of 67

New Words or Concepts...

  • Servo
  • Kinetic Machine
  • Microcontroller
  • Sensors (inputs)
  • Servos & Motors (Kinetic Outputs)

9

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

10 of 67

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

11 of 67

👉 Try This:

This is a Hands On Project. Try Stuff!!!!

  • Browser: You can use a web based Browser TinkerCAD. (SignUp Workshop Presentation)
  • Please take the time to build the example in TinkerCAD or on a real Breadboard.

11

print("Hisssssssssss")

Try This 👉

Try This

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

12 of 67

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

13 of 67

TinkerCAD - Join NOW

https://www.tinkercad.com/

Click JOIN NOW or Sign In

13

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

14 of 67

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

15 of 67

TinkerCAD - Learn About Electronic Circuits

https://www.tinkercad.com/

Click JOIN NOW or Sign In

15

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

16 of 67

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

17 of 67

Did You Say “Arduino”?

  • Arduino Is An Open-source Electronics Platform
    • With easy to use hardware & software.
    • Intended for making interactive projects
  • Arduino Can Sense & Control It’s Environment
    • Receiving information on input pins
    • Affecting its surroundings by controlling lights, motors, actuators, etc on output pins
  • You Tell Your Arduino What To Do
    • Writing code in the C++ coding language using the Arduino development environment

17

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

18 of 67

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

19 of 67

What Are We Going To Do?

  • Teams of Two
  • Kinetic Events and Contraptions
  • Microcontroller (Arduino) Code
  • Inputs - Switches and Sensors
  • Outputs - Electro-Mechanical (Servos & Motors)

19

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

20 of 67

Switch (Input) → Arduino → LED (Output)

20

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

21 of 67

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

22 of 67

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

23 of 67

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

24 of 67

Click on "CODE"

24

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

25 of 67

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

26 of 67

Lab#2 - Move LED Pin To Something Other Than Pin 13

  • Move your LED Output from pin 13 to Pin 2
  • Update your code

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

27 of 67

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

28 of 67

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

29 of 67

Lab #3 - Read A Switch Value

To find out if a button is being pushed, what do we need to do?

  • We need to assign a pin as an input���

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

30 of 67

Where Do We Connect The Switch Input?

30

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

31 of 67

Lab #4 - Now that we know the pin the sensor is assigned, how do we store the value?

  • We need a variable to hold the button_state��

31

int sensor_state = 0;

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

32 of 67

Lab #4 - How do we actually tell the Arduino to read an Input pin?

  • We need to invoke the digitalRead() function���

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

33 of 67

Lab #4 - Setup a Arduino Sketch to be able to print?

  • We need to add the Serial.begin(9600); method to the setup() function���

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

34 of 67

Lab: Switch, Read It, Save It, Do Something…

Finish the lab where you have done the following:

  • Connected a Breadboard LED to Pin 2, and can flash it
  • Connected a Button (Pulled Down) to Pin 7, and can read it
  • Print the value of the Button to the Arduino IDE Console
  • Turn on the LED when the Button is pushed

34

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

35 of 67

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

36 of 67

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

37 of 67

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

38 of 67

Sensor (Input) → Arduino

38

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

39 of 67

Arduino → Electro-Mechanical Device (Output)

39

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

40 of 67

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

41 of 67

Servo Motor Introduction

  • A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear position
  • This means, it is a motor that we can precisely control the position.
  • Servo motors are found in the motor unit of industrial, robotic, manufacturing appliances.

41

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

42 of 67

Two Types of Servos

  • 180° Servos - The most common Servo that will turn 180°
    • They will turn from 0° to 180° degrees
    • In many cases they can't actually get to 180° and will stop at something less, like 172°
  • Continuous Rotation Servos - These will turn continuously clockwise or counterclockwise for 360°
    • Many times you will find these on Robot drive systems

42

They Look The Same...

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

43 of 67

Servo Horns - Moving Stuff

43

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

44 of 67

Servo Horn Connection �Examples

44

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

45 of 67

Pulse Width Modulation

PWM

45

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

46 of 67

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

47 of 67

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 of 67

  • A servomotor is a closed-loop servomechanism that uses position feedback to control its motion and final position
  • This means, it compares the current position to the target position and then has the motor turn to go the target position

48

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

49 of 67

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

50 of 67

The PWM ON/OFF Time is Avaged to a Voltage

The Pulse Cycles is 20ms

  • The 0° position Pules = 1ms, which is 5% of 20ms or 5% of the max voltage
  • The 90° position Pules = 1.5ms, which is 7.5% of 20ms or 7.5% of the max voltage
  • The 180° position Pules = 2ms, which is 10% of 20ms or 10% of the max voltage

50

VMotor POS

VPWM

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

51 of 67

Actual PWM Voltage Comparator

  • A servomotor is a closed-loop servomechanism that uses position feedback to control its motion and final position
  • This means, it compares the current position to the target position and them have the motor turn to go there

51

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

52 of 67

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

53 of 67

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

54 of 67

Arduino Servo PWM Code

Setup the GPIO and Servo settings

  • my_servo.attach(9);is the library method to attach the servo to the Arduino pins. In this case it is attached pin 9, but all the digital pins can connect to servos.

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

55 of 67

Tell Your Servo Where To Start

  • A good practice when using Servos and other motors, is to tell them where to start. What is the default start position?
  • Before you tell the Servo to move as part of your main program, tell it where to start
  • Add this code just before you tell the Servo to actually do something. This way you know where you are starting.

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

56 of 67

Telling The Servo What Position (Angle) To Go To

A Servo methods in the Servo.h library is my_servo.write(0)�

  • Remember the Duty Cycle…

  • The PWM Duty Cycle can be a number between 0 - 100
  • It represents the % "On" time of the Pulses in the Duty Cycle
    • The 0° position is 1ms, about 5% of 20ms
    • The 90° position 1.5ms, about 7.5% of 20ms
    • The 180° position is 2ms, about 10% of 20ms

56

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

57 of 67

Arduino Servo PWM Code

  • my_servo is the variable name of your servo object and .write(n) is a servo library method
  • my_servo.write(0) tells the servo to move to position 0, by setting the minimum PWM width
  • my_servo.write(120) changes the PWM pulse width and moves to position 120

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

58 of 67

Lab Time

58

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

59 of 67

Hookup the Servo circuit

  • Pin 5V (VCC) to the Red wire
  • Pin (GND) to �the Black or Brown wire
  • Pin 9 to the Orange (Signal) wire

59

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

60 of 67

Hookup the Servo circuit & Add Sensor or Button Input

  • Update Code to Mix the Sensor Code and the Servo Code

60

等等,不要点击

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

61 of 67

61

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

62 of 67

Team Building & Negotiation Skills

62

STEAM Clown ™️ Production

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

63 of 67

What Materials Can You Use?

  • Duct Tape
  • Dominos
  • Blocks
  • Legos
  • Hot Glue
  • Cardboard
  • PVC
  • Marbles
  • Ping Pong Balls
  • Hot Wheels…

63

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

64 of 67

Design Thinking & Planning

What Will You Build?

  • Spend 10 minutes Planning
    • What materials will you need?
    • What Engineering challenges will you encounter?

64

10 Minutes

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

65 of 67

How To Launch?

65

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions

66 of 67

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

67 of 67

Thank You…

Questions?

67

Jim Burnham (AKA STEAM Clown)

  • Mechatronics Engineering Instructor at Silicon Valley Career Technical Education (SVCTE)
  • Engineering Teacher, Curriculum Developer, Workshop & Conference Presenter
  • LinkedIn - https://www.linkedin.com/in/jim-burnham-2243018/
  • Top Clown at TopClown@STEAMClown.org
  • Mechatronics Engineering Instructor (AKA Fire Breathing Robots)
  • Want to Support a Teacher? - Here Is How Can You Help

© Copyright - STEAM Clown TM

Creative Commons Licenses - BY-NC-SA 4.0

STEAM Clown TM Productions