start arduino course 04 - servos - stem toys & coding kits ...€¦ · start arduino course 04...

8
Introduction: In this session you will look at a piece of equipment called a Servo Motor. This is a type motor used to create really precise movement. Unlike DC (Direct Current) motors, which can only be specified to move forward or backward, Servo motors can specify a position to the degree. Goals Servo motor basics. Access the servo library. Control the Servo with a potentiometer. Start Arduino course 04 - Servos Page 1 - Start Arduino Course: 04 Servos

Upload: ledieu

Post on 18-Apr-2018

224 views

Category:

Documents


5 download

TRANSCRIPT

Introduction:

In this session you will look at a piece of equipment called a Servo Motor. This is a type motor used to create really precise movement. Unlike DC (Direct Current) motors, which can only be specified to move forward or backward, Servo motors can specify a position to the degree.

Goals

• Servo motor basics.• Access the servo library.• Control the Servo with a potentiometer.

Start Arduino course 04 - Servos

Page 1 - Start Arduino Course: 04 Servos

The SensorsTo use a Servo motor, we need two things, we need to be able to power it and to control it. This means they’ll be three connections to our Servo motor: Power, Ground and Control. To control Servos, we actually use the same principle for controlling the brightness of our LEDs – Pulse Width Modulation. This works by changing the duty cycle of the square wave, and keeping the frequency the same throughout.

Pulse Width Modulation (PWM)

Start Arduino course 04 - Servos

Page 2 - Start Arduino Course: 04 Servos

5v

25% Duty Cycle 50% Duty Cycle

75% Duty Cycle

5v

5v

0v 0v

0v

What does the Arduino Pin have to allow if we want to use a servo with it?

What symbol does the Arduino use to identify which pins can do this?

04 - Servos Challenge

For this circuit, we are going to create a knock sensor. This will detect vibration and convert this into an output for an LED. As our Piezo transducer using an Analog input, this means its value can be more than just High or Low. So we will use the Analog In pins.

For this, we will need:

1x Arduino Uno1x USB cable1x Servo motor6x Jumper Wires

Circuit 4.0 - Servo Hardware

Start Arduino course 04 - Servos

Page 3 - Start Arduino Course: 04 Servos

Make a new sketch in the Arduino IDE and name this ‘Servo’.

To use the servo, there is a lot of complex algorithms and conversions to make sure the servo can make these precise movements, so we’re going to use the Servo Library which comes included in Arduinos IDE. To use external library, you have to let the Arduino know you want it included in your sketch. To do this we use the <include> function.

//include the Servo library in the sketch#include <Servo.h>

//create servo object to control a servoServo servo1;//variable to store the servo positionint pos = 0;

void setup(){ //attaches the servo on pin 9 to the servo object servo1.attach(9);}

void loop(){ //goes from 0 degrees to 180 degrees in steps of 1 degree for (pos = 0; pos <= 180; pos +=1) { //tell servo to go to position in variable ‘pos’ servo1.write(pos); //waits 15 milliseconds for the servo to reach the position delay(15); } //goes from 180 degrees to 0 degrees for (pos = 180; pos >= 0; pos -= 1) { //tell the servo to go to position in variable ‘pos’ servo1.write(pos); //waits 15 milliseconds for the servo to reach the position delay(15); }}

Start Arduino course 04 - Servos

Page 4 - Start Arduino Course: 04 Servos

Circuit 4.0 - Servo Code

Start Arduino course 04 - Servos

Page 5 - Start Arduino Course: 04 Servos

Circuit 4.0 - Servo Code

Once you have copied the code, press (compile) and if no errors appear, press (upload) and watch the results!

At this stage your servo should be rotating backwards and forwards.

My code won’t compile! Is everything spelt correctly? Are all your lines ending in a semi-colon? Do you have the correct capital letters?

Did you close the curly brackets?

Let’s go through the code understand what every part is doing.

#include <Servo.h>

Servo servo1;

void setup()

int pos = 0;

Libraries

This is how we include the Arduino Servo Library. This makes it possible to use pre-defined functions in our projects that make using the Servo motors and Arduino easier.

Servo Object

This is how we make our Servo Object . This allows us to use the library functions. We will now refer to our servo as servo1.

void setup()

This is our void setup() . We use curly brackets to define what happens in the setup of our program. This function is called once.

Global Variables

These are our global variables for storing the Servo position and Servo pin. This means we can easily keep track of the position and the digital pin we are using for controlling the Servo. These are created outside void setup and void loop so they are accessible in both.

servo1.attach(9);

void loop(){

//goes from 0 degrees to 180 degrees in steps of 1 degree for (pos = 0; pos <= 180; pos +=1) { //tell servo to go to position in variable ‘pos’ servo1.write(pos); //waits 15ms for the servo to reach the position delay(15);

//goes from 180 degrees to 0 degrees for (pos = 180; pos >= 0; pos -= 1) { //tell the servo to go to position in vari-able ‘pos’ servo1.write(pos); //waits 15ms for the servo to reach the position delay(15);

Our Servo needs to be attached to the digital pin we are using to control it. We set this using the number stored in variable servo_pin. This called in void setup() as it only needs to called once.

void loop()

This is our void loop(). This is where our main loop for our program happens. We set everything inside void loop() by using curly brackets. Everything within these brackets will loop infinitely.

for loop (forwards)

This is our for loop for moving our servo forwards. We use normal brackets to set how many times to loop through everything within our curly brackets. In this instance, we increment the variable pos by 1 until it reaches 180. Everytime we increment pos, we use Servo library function write(). Variable pos is then used to set the Servo Position. We use delay() with a value of 15 to pause the loop for 15 milliseconds. This gives the Servo time to reach it’s position.

for loop (backwards)

This for loop is for moving our Servo position back-wards. This works exactly the same as the previous for loop, but instead of moving from 0 to 180, it sets the Servo Position from 180 to 0. This loop happens after the first, so variable pos can first reach 180, then will decrease to 0.

Start Arduino course 04 - Servos

Page 6 - Start Arduino Course: 04 Servos

How would you change the speed of your Servo?

What happens when we remove the delay in our for loops? Why?

What other digital pins could I use to control a Servo with? How do we know this?

What could you make with your Servo?

Start Arduino course 04 - Servos

Page 7 - Start Arduino Course: 04 Servos

Circuit 4.0 - Servo Challenge

Can you try and control the Servo with a potentiometer?

For this, we will need:

1x Arduino Uno1x USB cable1x Servo motor8x Jumper Wires1x Potentiometer

Circuit 4.1 - Servo + Potentiometer Hardware

Start Arduino course 04 - Servos

Page 8 - Start Arduino Course: 04 Servos

Try and work the code out for yourself

Remember back to how we read data from a potentiometer, it uses Analog Readings. That means you will need to scale your Analog Input data to the Servo position data. For this you can use the Arduino function map(). This function is an easy way of scaling a value to another range.

What is the input?

What is the minimum and maximum value the input can have?

What is the minimum and maximum value the outcome should have?

int outcome - map(int input, int min_input, int max_input, int min_output, int max_output);