Making the ball play / pre-programmed system
OK, so your Ardiono blinks, that was Hello World!
Now we will hookup a servo motor and instruct it to behave a certain way.
Servos have to receive high-pulse control signals at regular intervals to keep turning. If the signal stops, so does the servo.
Once your sketch uses the Servo library to set up the signal, it can move on to other code, like delays, checking sensors, etc. Meanwhile, the servo keeps turning because the Servo library keeps running in the background. It regularly interrupts the execution of other code to initiate those high pulses, doing it so quickly that it’s practically unnoticeable.
An ordinary DC motor has two hookup wires and simply turns continuously when power is applied. If you want it to spin in the opposite direction, you'll need to reverse the power. And if you want to know how far it has turned, you'll need to devise a way to measure that.
RC servos are reasonably standardized - they are all a similar shape, with mounting flanges at each end, available in graduated sizes, from "ultra-nano" to "giant". Servos often come with multiple attachments, such as wheels or levers, known as “horns”, than can be attached to the shaft, to fit the device they are operating.
The servo has 3 wires, we need to connect them all to the arduino. One thing that can be confusing is that the wiring color code isn't always consistent -- there are several color codes at play. The good news is that the pins are usually in the same order, just that the colors are different.
If you're in doubt about your color scheme, check the documentation -- don't plug it in backwards!
red is for 5V
you will find it easy to plug one end of a jumper wire inside the connectors of the servo motor, and the other end to the corresponding pin of the Arduino make sure you use corresponding colors for the jumpers, in bigger setups messy wire can cause you more time to debug
black is for GND
to GND of the Arduino
Regardless of how you're powering them, it's worth noting that the current consumed by the motor increases as the mechanical loading increases. A small servo with nothing attached to the shaft might draw 10 mA, while a large one turning a heavy lever might draw an Ampere or more! If your power supply isn't up to the task, a straining or stalled servo can cause the supply to sag, which may have other unpredictable repercussions, such as causing microcontrollers to reset.
and orange is for signal
signal is going into the pin of the Arduino we will use to control the servo motor
We will look for a pin that has a wave next to the number ( look at the Arduino board). Those pins are able to output Pulse Width Modulation (PWM) is a fancy term for describing a type of digital signal. Pulse width modulation is used in a variety of applications including sophisticated control circuitry. Also in our case control the servo motor. The control wire is used to send this pulse.
This control signal is a specific type of pulse train. The pulses occur at a 20 mSec (50 Hz) interval, and vary between 1 and 2 mSec in width. The Pulse Width Modulation hardware available on a microcontroller is a great way to generate servo control signals.
Common servos rotate over a range of 90° as the pulses vary between 1 and 2 mSec -- they should be at the center of their mechanical range when the pulse is 1.5 mSec.
For more info how servos work look here []
Internally, the mechanism of a servo motor uses a potentiometer attached to the rotating shaft to sense the position. It measures the width of the incoming pulse and applies current to the motor to turn the shaft, until the potentiometer indicates that the position corresponds to the incoming pulse width. This is a form of feedback control. The motor has received the desired position from the pulse width, and the actual shaft position is fed back to the circuit via the potentiometer. It compares the desired value to the actual value and drives the motor in the direction that causes actual to match desired.
We will use an example code to see if our servos work
Now with knowing where the examples are located, find a servo example sketch called sweep.
you should have a code that looks like this
Let's upload the sketch to the board
Observe the motor---->sweeping?