Saturday, 19 October 2024

Joystick-controlled Car with Arduino

This article is about the movements of joystick-controlled car. We began with calibrating the front, back, left and right movements. We realized that when the joystick is moved to front, the reading of x coordinate is 0. When the joystick is moved back, the reading of x coordinate is 1023. When the joystick is moved to left, the reading of y coordinate is 1023. Finally, when the joystick is moved to right, the reading of y coordinate is 0.

NOTE: At this point, we have Joystick alone connected with Arduino for calibration. We have not yet integrated the joystick movements with the car.

After calibration of the movements with the joystick, we moved towards integrating joystick with the Car. Please refer to the circuit diagram below which illustrates the basic circuit required for car movements as well as it's integration with joystick.

After completing the integration, we wrote a cpp program with Arduino IDE. The basic programming logic is:
If (X == 0) move the car front If (X == 1023) move the car back If (Y == 0) move the car left
If (Y == 1023) move the car right

Please take a look at our small demo of the Joystick Controlled Car below.




We will discuss the following points further in this article in detail:
  1. Create a Robotic Car with Arduino, L293D motor shield and Four AA Batteries
  2. Integrate the Joystick with the Car
  3. Explanation of the CPP program to drive Arduino and the Joystick

Let's discuss the first point, where we create the robotic car. We used a DIY (Do It Yourselves) kit from the local robotics store. The DIY kit looks like below.



It consists of the following components


We also purchased a switch to connect and disconnect the batteries from the rest of the circuit. Our switch looks like below. 


I am listing the specifications for the switch below just for reference:



We assembled our car as per the instructions provided in the DIY kit. We assembled the circuit for Joystick-Controlled Car as shown in the below circuit diagram. 


NOTE: I have assumed that the readers of this article will know the basic electronics symbols of the circuit and the switch. However, let me give a brief introduction of the battery symbol over here. The battery symbol is shown with two parallel vertical lines with one line smaller than the other, placed next to each other over around 2mm distance. And the switch symbol is denoted using two split lines at an angle over each other, which symbolizes no-connection. I am listing down the steps of the assembly below. 

  • We stacked Motor-Shield over Arduino-Uno. 
  • The AA batteries are placed into the battery holder provided with DIY chassis. 
  • The red wire of the battery holder is connected to +M of the shield (As indicated by the "+" sign in the circuit diagram)
  • The black wire of the battery holder is connected to I end of the switch
  • O end of the switch is connected to ground of the shield (As indicated by the "-" sign in the circuit diagram)
  • The black and red wires are soldered to both the yellow DC motors (provided with the chassis) as shown in the below figure. The wires are then connected to the M3 and M4 ports of the shield. The polarity of the connections matter, in the sense that if we reverse the connections, the motors will rotate in opposite directions. As a result, our car moves in opposite direction. So we need to first decide which way we want our car to move and then make the connections. 
  • To know more about the Arduino-L293D stacking and about the motor connections, please refer to video stacking-and-motor-connections. It is a great source of information. 





For your reference, we are sharing a picture of the joystick which is compatible with Arduino Uno below.



Now, let's discuss the second point, which is the integration of Joystick with Arduino. Joystick has totally 5 pins to integrate with Arduino: GND, +5V, VRx, VRy, SW.
  • The GND of the Joystick is connected to GND of Arduino
  • The +5V is connected to +5V of Arduino
  • VRx is connected to analog pin A0 of Arduino
  • VRy is connected to analog pin A1 of Arduino
  • SW is connected to digital pin 2 of Arduino
NOTE: The reason VRx and VRy is connected to Analog pins and NOT Digital pins is unknown to me. Most probably, the joystick gives us continuous signal of movement. The signal is not binary (HIGH/LOW). That is the reason we have connected it to analog input (according to the sample program given in the references section).

With this, we complete the description of the hardware part of the Joystick controlled car. Now let's look at the third point, which is the software part. The CPP program which needs to be uploaded in Arduino Uno is shared below. As we know, the Arduino program execution takes place in two parts "setup" and "loop". The setup function does the following tasks:

  • Open serial communication at baud rate 9600 bps
  • Set the speed of the motors at M3 and M4 to 200. This number "200" is a parameter which controls the speed of the motor. It does not have a unit. But based on this number the power supplied to the motor varies and hence the speed
  • And by default, the switch (SW) pin is set to high. This will make the program read a HIGH input when we press the Joystick switch
Now, let us look at the function of loop:
  • It checks whether VRx pin is reading a zero (when the joystick is rotated towards forward direction). The detection of zero means that the X movement of car is in the forward direction. So set the motor 3 and 4 to rotate forward and the car will move forward
  • It checks whether VRx pin is reading a 1023 (when the joystick is rotated towards backward direction). The detection of 1023 means that the X movement of car is in the backward direction. So set the motor 3 and 4 to rotate backward and the car will move backward
  • It checks whether VRy pin is reading a zero (when the joystick is rotated towards right direction). The detection of zero means that the Y movement is in the right direction. So set the motor 3 and 4 to rotate right and the car will move towards right
  • It checks whether VRy pin is reading a 1023 (when the joystick is rotated towards left direction). The detection of 1023 means that the Y movement is in the left direction. So set the motor 3 and 4 to rotate left and the car will move towards left
  • If the Switch button of the joystick is pressed downwards, the SW pin is read HIGH and the motors are released and the car stops
NOTE: We had also tried performing a small experiment to increase the speed of the car with the joystick. However, that was not successful because we never had a 6th input apart from the above 5 inputs (which are forward, backward, left, right and down). If we had another button or another kind of movement apart from X and Y, we would have implemented this scenario successfully. Hence our request to all the readers is to ignore the 6th if statement in the program. This 6th if statement reads the SW_pin, negates the input and if it is true, increase the car speed to 250.

NOTE: Also, we have put all the direction control code in the loop function. This will help to detect the instantaneous joystick input and the car will be able to make a decision in a continuous manner.

NOTE: Please excuse us to choose driving a car with joystick, for which we needed a visible mess of wires. Ideally we should choose something else to drive with joystick.... something which does not have much physical movement. May be a computer game or something. But let's keep it for next time!







References


No comments:

Post a Comment