Learn how to build and code a circuit to control the position of 2 servo motors using only 1 joystick.
Remember, a joystick has 2 potentiometers, so each potentiometer will be related to a different servomotor. The position of the servomotor will vary from 0º to 180º depending on the movement of the joystick, from left to right.
DIFFICULTY LEVEL: Intermediate.
DURATION OF THE EXERCISE: 30 min.
- 1 Joystick
- 2 Servomotors
- 1 Build&Code UNO board
- 1 Build&Code sensor shield
- 1 USB – USB type B cable
- Connect the joystick to the analog ports A0 and A1 of the Build&Code 4in1 board.
Connections to the analog port A0:
- Connect the GND to the G pin.
- Connect the Vcc to the V pin.
- Connect the VRx to the S pin.
Connections to the analog port A1:
- The VRy to the S pin.
As you remember, we’ve connected the joystick to the analog ports A0 and A1 because it has two potentiometers, which are analog sensors.
- Connect the servomotors to the Build&Code 4in1 board.
- Connect the servomotor S1 to the digital port 6. Connect the brown cable to the G pin, the red cable to the V pin and the orange cable to the S pin.
- Connect the servomotor S2 to the digital port 9. Connect the brown cable to the G pin, the red cable to the V pin and the orange cable to the S pin.
We’ve connected the servomotors to the digital ports 6 and 9 because they have a PWM output. This output will be in charge of making the servomotor rotate more or less depending on the values received from the joystick.
- The following image shows the connections. Use it as a guide.
You can do this project using the Arduino program or a visual programming software by blocks compatible. Below you will find the necessary code.
You will create a program that will be constantly reading information from the 2 joystick outputs, and basing on the reading received from the joystick, it will make the servomotor move to one position or the other. When you release the joystick, the servomotor will go back to its original position.
Before start, remember that:
- A potentiometer is an analog sensor, so it will make readings from 0 to 1023, basing on how much you have rotated it.
- The digital pins PWM will be working as output pins, but instead of giving a permanent high or low value, they will be able to make very quick changes in order to give medium values too. This will make possible to send different levels of output voltage.
- To work with servomotors, you will use the “#include” servomotor library, since it will make it much easier.
- A servomotor can rotate from 0º a 180º, so you will have to relate the analog reading values with the servomotor movement values, using the “map” instruction.
Now, follow this steps:
- Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
- Configure and upload the code, following the indications on the Arm Robot First Steps guide.
- Open the Arduino program and copy the following code in it:
Click on the magnifier to see how the variations of the joystick affect the position of the servomotor.
Code for the visual programming software by blocks compatible
- Download and install the program.
- Open the software.
- Configure the program to save code into the Build&Code UNO board. You will find the instructions in the Arm Robot First Steps guide.
- Open the program and copy the following code:
- To vary the position of the servomotor according to the movement of the joystick, you will do the following mathematical operation:
Value of the servomotor = (Value of the joystick * highest value of the servomotor) / highest value of the joystick
Servomotor S1 = (Reading of the analog port A0 * 180)/1023
The servomotor works with values from 0 to 180 and the joystick gives values from 0 to 1023. In order to move the servomotor, you have to relate the values of the joystick.
To activate the program you have to configure the control board in a different way. Follow these steps:
- Go to the Boards section and select Arduino UNO.
- Go to Connect and select Serial Port. Then, select the COM port to which the Build&Code UNO board is connected.
- In Connect, select Update Firmware and wait until the communication program is completely downloaded.
Once the program is finished, click on the green flag, move the joystick and watch the Arm Robot moving.
RESULT OF THE EXERCISE
The program will be constantly reading information from the 2 joystick outputs and, basing on the reading received from the joystick, it will move the servomotors to one position or the other, from 0º to 180º. When you release the joystick, the servomotors will go back to their original position (90º).