Build and code a circuit to control the position of 2 servo motor using only 1 joystick.
Remember that a joystick has 2 potentiometers, so each potentiometer will be related to a different servomotor. When moving the joystick up and down you will control one servomotor, and when moving the joystick to the left and to the right you will control the other servomotor.
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, with 5º variations. In this case, when you release the joystick, the servomotor will stay in that 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.
- To make the servomotor move to one position or the other, use the conditional instruction “if/else”.
- If the reading value of the X or Y axis or the joystick is higher than 550, you will be moving the joystick forward, so you will have to relate this condition to a positive movement of the servomotor. This means that the position of the servomotor will increase 5º.
- If the reading of the joystick on the X or Y axis is lower than 480, you will be moving the joystick backwards, so you will have to associate this condition to a negative movement of the servomotor. This means that you will be subtracting 5º to the current position of the servomotor.
- If the reading of the servomotor is between 480 and 550, this means you are not moving the joystick. In this case, you don’t have to variate the degree’s variable that modifies the position of the servomotor.
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:
Code for the visual programming software by blocks compatible
- Download and install the program.
- To create this code you will use variables. The variables are a block to which you will assign a name. In that block you will save values.
- The variable blocks are located in Data and Blocks. To create a variable click on Create a variable, name it and you will have blocks available for that variable.
- The variables you will create are called DEGREE1 and DEGREE2. They will have the degree’s values from servomotors 1 and 2, which will be modified basing on the movement of the joystick.
- Open the program and copy the following code:
- Configure the program to save code into the Build&Code UNO board. You will find the instructions in the Arm Robot First Steps guide.
RESULT OF THE EXERCISE
The program will be constantly reading information from the 2 joystick outputs: VRx and VRy.
If there is an increase to a value higher than 550, the servomotors will increase 5º, and when there is a decrease to a number lower than 480, the servomotors will subtract 5º, until reaching the highest and lowest values: 180º and 0º.
The increment and decrement of the values of the joystick correspond to the forward, backward, left and right movements. If the joystick remains static, the servomotor will keep its last position.