Home > Tech Article Categories > R3actor and Arduino > Servo Control with R3aktor
Hobby servos are a great way to add physical motion to your projects. They typically have three wires: 5V power, ground, and signal. The servo position is commanded by electrical pulses on the signal wire. Short pulses (about 1ms) drive the servo all the way one direction, while long pulses (2ms) drive it to the other side. A 'centered' pulse (1.5ms) commands the servo to move to the center position, between the two end stops.
R3aktor is capable of driving the servo signal wire directly. Due to possible current requirements, the servo itself should be powered by a separate power supply rather than the 5V USB power from a PC to avoid damaging the computer's port.
For this project, you will need:
Place the breadboard power supply into the breadboard. Connect the wires of the servo to their respective locations on the breadboard. Red to the red (+) power rail on the breadboard, brown to the blue (-) power rail on the breadboard, and orange to any location on the breadboard. Connect the R3aktor GND pin to the blue (-) power rail on the breadboard. Connect digital pin 9 (D9) on the R3aktor board to the same rail as the servo’s orange wire is connected to. Once complete, your circuit should look like the diagram below. Confirm that the GND pin on the breadboard power supply module is in the blue (-) rail and the VCC pin on the power supply is in the red (+) rail.
Open the Arduino IDE. For this project, you will need to utilize the Arduino Servo library which comes pre-installed with the IDE, so there is no library installation you need to do. Copy and paste this code into a new sketch:
Connect the R3aktor board to your computer with the USB-C cable.
In the Arduino IDE, select the R3aktor board. Then select upload.
Most breadboard power supply modules will have a switch on them. Once the power supply module is connected to an AC to DC wall wart, the switch can be turned on and the servo motor will begin to sweep back and forth, stopping for a moment between each movement. You may notice that the movement speed of the servo is constant at all angles. This can lead to a “jerkiness” effect when changing directions in a short period of time.
Now, comment out the delay() statements on lines 15 and 22 to see this effect in action:
As you can see, the servo is unable to reach the desired range of motion, and the rapid change of direction causes unwanted vibrations. To fix this, a technique known as “servo dampening” can be applied. This method decreases the servo’s speed as it nears its goal angle, decreasing the inertia of the motor and leading to the change in direction causing less vibrations.
Because this is a common issue, there is already a library that allows you to control a servo’s acceleration. This library is called “ServoEasing”. To utilize it, you will first need to install it.
Open the library manager in the Arduino IDE by selecting Tools -> Manage Libraries…
In the search box, type ServoEasing
Hover your mouse over the ServoEasing library and select install.
Then, wait for the process to complete.
Copy this code to your Arduino IDE:
Make sure to select the R3aktor board and select upload.
This code goes through a few options of controlling servo movement allowed by the ServoEasing library. Each movement type has its own benefits, it all depends on the project requirements.