0

members

Easy Arduino Starter Kit: Project 5 MOOD CUE

of Arduino in UNO

Arduino Starter Kit: Project 5

Project name: MOOD CUE

Discover: mapping values, servo motors, using built-in libraries

Attachments: libraries and sketch

In this project, you needed these parts :

1.Aruduino Uno R3 (you can also use the other version of Arduino)

2.Jumper cables

3. Potentiometer 1 pc 

4. Breadboard half size 

5. Arduino IDE ( you can download it from here  )

6. Capacitors 100 uF 2 pcs

7. SG90 micro servo with motor arm

8. Male header pins (3 pins)

GENERAL

Servo motors are a special type of motor that do not spin around in a circle, but move to a specific position and stay there until you tell them to move again. Servos usually rotate 180 degrees only (one half of a circle). Similar to the way you used pulses to PWM an LED in the Color Mixing Lamp Project, servo motors expect a number of pulses that tell them what angle to move to. The pulses always come at the same time intervals, but the width varies between 1000 and 2000 microseconds. While it’s possible to write code to generate these pulses, the Arduino software comes with a library that allows you to easily control the motor. Because the servo only rotates 180 degrees, and your analog input goes from 0 to 1023, you’ll need to use a function called map() to change the scale of the values coming from the potentiometer. There are libraries for a wide variety of sensors and actuators and other devices with Arduino. An Arduino software library expands the functionality of a programming environment. The Arduino software comes with a number of libraries that are useful for working with hardware or data. One of the included libraries is designed to use with servo motors. In your code, you import the library, and all of its functionality will be available to you.

THE CIRCUIT

Attach 5V and ground to one side of your breadboard from the Arduino. Place a potentiometer on the breadboard, and connect one side to 5V, and the other to ground. A potentiometer is a type of voltage divider. As you turn the knob, you change the ratio of the voltage between the middle pin and power. You can read this change on an analog input. Connect the middle pin to analog pin 0. This will control the position of your servo motor. The servo has three wires coming out of it. One is power (red), one is ground (black or brown), and the third (white or orange or yellow) is the control line that will receive information from the Arduino. Plug three male headers into the female ends of the servo wires.

Connect the headers to your breadboard so that each pin is in a different row. Connect 5V to the red wire, ground to the black wire, and the white wire to pin 9. When a servo motor starts to move, it draws more current than if it were already in motion. This will cause a dip in the voltage on your board. By placing a 100 uf capacitor across power and ground right next to the male headers, you can smooth out any voltage changes that may occur. You can also place a capacitor across the power and ground going into your potentiometer. These are called decoupling capacitors because they reduce, or decouple, changes caused by the components from the rest of the circuit. Be very careful to make sure you are connecting the cathode to ground (that is the side with a black stripe down the side) and the anode to power. If you put the capacitors in backwards, they can explode. Your servo motor comes with female connectors, so you need to add header pins to connect it to the breadboard.

THE CODE

To use the servo library, you’ll first need to import it. This makes the additions from the library available to your sketch. To refer to the servo, you’re going to need to create a named instance of the servo library in a variable. This is called an object. When you do this, you’re making a unique name that will have all the functions and capabilities that the servo library offers. From this point on in the program, every time you refer to myServo, you’ll be talking to the servo object. Set up a named constant for the pin the potentiometer is attached to, and variables to hold the analog input value and angle you want the servo to move to. In the setup(), you’re going to need to tell the Arduino what pin your servo is attached to. Include a serial connection so you can check the values from the potentiometer and see how they map to angles on the servo motor. In the loop(), read the analog input and print out the value to the serial monitor. To create a usable value for the servo motor from your analog input, it’s easiest to use the map() function. This handy function scales numbers for you. In this case it will change values between 0-1023 to values between 0-179. It takes five arguments : the number to be scaled (here it’s potVal), the minimum value of the input (0), the maximum value of the input (1023), the minimum value of the output (0), and the maximum value of the output
(179). Store this new value in the angle variable. Then, print out the mapped value to the serial monitor. Finally, it’s time to move the servo. The command servo. write() moves the motor to the angle you specify. At the end of the loop() put a delay so the servo has time to move to its new position.

You first need to import the servo library. This makes the additions from the library available to your sketch. To refer to the servo, you need to create a named instance of the servo library in a variable. This is called an object. When you do this, you are making a unique name that will have all the functions and capabilities that the servo library offers. From this point on in the program, every time you refer to myServo, you talk to the servo object. Set up a named constant for the pin the potentiometer is attached to, and variables to hold the analog input value and angle you want the servo to move to. In the setup(), you need to tell the Arduino what pin your servo is attached to. Include a serial connection so you can check the values from the potentiometer and see how they map to angles on the servo motor. In the loop(), read the analog input and print out the value to the serial monitor. To create a usable value for the servo motor from your analog input, it is easiest to use the map() function. This handy function scales numbers for you. In this case it will change values between 0 - 1023 to values between 0 - 179. It takes five arguments : the number to be scaled (potVal), the minimum value of the input (0), the maximum value of the input (1023), the minimum value of the output (0), and the maximum value of the output (179). Store this new value in the angle variable. Then, print out the mapped value to the serial monitor. Finally, we can move the servo. The command servo.write() moves the motor to the angle you specify. At the end of the loop() put a delay so the servo has time to move to its new position.

 

THE START

Once your Arduino has been programmed and powered up, open the serial monitor. You should see a stream of values similar to this: potVal : 1017, angle : 171. When you turn the potentiometer, you should see the numbers change. You should see your servo motor move to a new position too. Notice the relationship between the value of potVal and angle in the serial monitor and the position of the servo. You should see consistent results as you turn the pot. One nice thing about using potentiometers as analog inputs is that they will give you a full range of values between 0 and 1023. This makes them helpful in testing projects that use analog input. Servo motors are regular motors with a number of gears and some circuits inside. The mechanics inside provide feedback to the circuit, so it is always aware of its position. While it may seem like this is a limited range of motion, it’s possible to get it to make a wide variety of different kinds of movements with some additional mechanics.

THE SUMMARY

Servo motors can easily be controlled by the Arduino using a library, which is a collection of code that extends a programming environment. Sometimes it is necessary to repurpose values by mapping them from one scale to another.

See one of movies on Youtube about it - link here



Other projects of Arduino

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 15-08-2017
Viewed: 1094 times