0

members

Easy Basics: Project 040c How to build the 4WD smart robot car Part 3 - Wireless PS2 controller, L298N H-

of Lex C. in UNO

Basics: Project 040c

 Project name:  How to build the 4WD smart robot car Part 3 - PS2 controller, L298N H-Bridge

Tags: Arduino, 4WD smart robot car, Arduino Uno R3, L298N H-Bridge motor driver module, PS2 controller controlled robot car, PlayStation 2 controller, 2.4G Wireless game gamepad joystick for PS2 controller, Sony playstation 2 console, dualshock gaming joypad for PS 2 play station, Wireless PS2 controller, PS2 controllerRF signal, LED

Attachments: library1, sketch1

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1.Arduino Uno R3 (you can also use the other version of Arduino)

2. Wireless PS2 controller  1pc

3.Arduino IDE ( you can download it from here  )

4.Jumper cables M-M, F-M

5. Breadboard (any size) 1 pc

6. Resistor 10 pcs (4 pcs 1KOhm and 6 pcs 220 Ohm)

7.4WD smart robot car set 1pc

8. L298N H-Bridge motor driver module (as you see they can be a little bit different but have the same working principle) 1pc

9. 6 x 1.5V AA Battery Box Case Holder with 6 pcs 1.5V AA batteries or Rechargeable 7.2V Power Supply (6 x 1.2V 1900 mAh) for motor driver module supply 1 pc

 

10. Arduino board power supply, can be from +7 to +12V DC (we used 9V battery with 9V Battery Clip and Barrel Jack) or 5V+ DC

 or

11. LED 6 pcs (2 pcs white for forward lights, 2 pcs red for back lights, 1 pc yellow for left turn, 1 pc blue for right turn)

12. Active Piezo buzzer 1 pc

13. AAA battery for PS2 controller 2 pcs

14. Power switch 1 pc

15. Soldering Iron

General

We will learn how to connect different components and make wireless PS2 controller controlled 4WD smart robot car. 

WHAT IS A ROBOT?

Robot is an electromechanical device which is capable of reacting in some way to its environment, and take autonomous decisions or actions in order to achieve a specific task.

A robot is consists of following components:

1. Structure / Chassis

2. Actuator / Motor

3. Controller

4. Inputs / Sensors

5. Power Supply

The Structure consists of physical components. A robot has one or more physical components that move in some way to perform the task. In our case the Chassis and wheels are the structure of the robot.

An “Actuator” can be defined as a device that converts energy (in robotics, that energy tends to be electrical) into physical motion. Most of the actuators produce either rotational or linear motion. In our case the actuator is DC gear motor. It is basically a DC motor combined with a gearbox that works to decrease the motor’s speed and increase the torque. Example: A DC motor having speed 3000 rpm and torque 0.002 Nm. Now we add a gear to it with gear ratio 1:48. The new speed is reduce by a factor 48 (resulting 3000/44 = 68 rpm) and the torque increased by a factor of 48 (resulting 0.002 x 48 = 0.096 Nm).

Structure and Actuators without Controller means nothing. Your robot will still sit in one place. Its just like a human without life. So a Controller (brain) needed to move the robot from one place to another. It is a computing device capable of executing a program and is responsible for all computations, decision making, and communications. In our case, we are using an Arduino Uno microcontroller as a Controller. The Controller takes input (sensors, remote etc), process it and then gives a command to the actuator (motor) to do the desired task.

Unlike humans, robots are not limited to just sight, sound, touch, smell and taste. Robots use different Sensors to interact with the external world. A sensor is a device that detects and responds to some type of input from the physical environment. The specific input could be light, heat, motion, moisture, pressure, or any one of a great number of other environmental phenomena. The Inputsmay be from sensors, IR remote control or smartphone/tablet/PC. We will use IR remote control as a input device to control the 4WD smart robot car.

A robot needs a Power source to drive the actuators ( motors ) and the controller. Most of the robots are powered by a battery. When we talk about battery, there are lot of options:

  • AA Alkaline Battery ( Non Rechargeable )
  • AA NiMh or NiCd Battery ( Rechargeable )
  • Li Ion Battery
  • LiPo Battery

So according to the requirement choose the suitable one. In our opinion, always choose a rechargeable and sufficient capacity battery.

Understanding the wireless PS2 controller

The PS2 wireless controller is a standard controller for the PlayStation 2 and is identical to the original DualShock controller for the PlayStation console. It features twelve analog (pressure-sensitive) buttons ( Χ, O, Π, Δ, L1, R1, L2, R2, Up, Down, Left and Right), five digital button (L3, R3 Start, Select and the analog mode button) and two analog sticks. The controller also features two vibration motors, the left one being larger and more powerful than the one on the right. It is powered by two AAA batteries. It communicates with the console using 2.4 GHz RF protocol.

PS2 wireless controller communicates with Arduino using a protocol that is basically SPI. The play station sends a byte at the same time as it receives one (full duplex) via serial communication. There’s a clock (SCK) to synchronize bits of data across two channels: DATA and CMD. Additionally, there’s a “Attention” (CS) channel which tells the slave whether or not it is “active” and should listen to data bits coming across the CMD channel, or send data bits across the DATA channel (Reasonably, only one slave device should be active at a time) . The PlayStation 2 actually uses this plus an additional line that is not specifically part of the SPI protocol – an “Acknowledge” (ACK) line.

The clock is held high until a byte is to be sent. It then drops low (active low) to start 8 cycles during which data is simultaneously sent and received. The logic level on the data lines is changed by the transmitting device on the falling edge of clock. This is then read by the receiving device on the rising edge allowing time for the signal to settle. After each Command is received from the controller, that controller needs to pull ACK low for at least one clock cycle. If a selected controller does not ACK the PS2 will assume that there is no controller present. LSBs (least significant bits) are transmitting first.

Key features:

 

  • High-quality ABS material
  • 2.4 GHz wireless reception technology, 8-meter range
  • Digital/analog, two modes of operation, can be switched at any time, with mode prompt light
  • 4-axis, 12 keys designed to meet the needs of game keys (digital mode: 2-axis 10 keys; analog mode: 4-axis 12 keys).
  • A receiver, two interfaces.
  • Needs to use two AAA (7) batteries (not included)

 

Specifications:

 

  • Handle model: NJP215B
  • Handle brand: Nygacn Nika
  • Product weight 173.5 grams
  • Working range: ≤8 meters
  • Operating current: 12 mA
  • Vibration current: 120 mA
  • Standby current: 20 μA
  • Dimensions: 15.7 x 9.3 x 6 cm
  • Function keys: Arrow keys + 11 common keys + 2 rockers
  • Battery: 2 AAA (7) batteries (not included)
  • Packaging: High-frequency blister packaging
  • Color: Black

 

Product includes:

 

  • 1 x Wireless controller
  • 1 x Receiver

 

Interface: For Sony PlayStation 2 host

Application:

  • To control a wheeled or tracked vehicle's motion.
  • To control a robotic arm.
  • To interact with a PC.
  • To control a pan and tilt camera mount.
  • DIY radio control system by using a bluetooth module, or radio module to transmit the commands from the Playstation 2 controller to another Arduino which is controlling the vehicle.
  • The pilot interface for an underwater drone.

Useful information about components

1. You can find information on how to assemble 4WD smart robot car set in How to build the 4WD smart robot car Part 1.

2. Understanding the L298N H-Bridge motor driver module

You can read more information here.

3. Understanding the DC motor

You can read more information here.

Signals and connections of the LED

The operating voltage of the LED is 1.8V and the operating current is 10mA-20mA. The Arduino Uno board can supply 5V or 3.3V power. We will use 5V for this project, so the minimum resistance of the current limiting resistor should be (5 V to 1.8 V)/20 = 160 Om. The 220 Om offered in the kit is suitable and you can also choose other resistors that meet the condition. The larger the resistance is, the dimmer the LED will get.

Signals and connections of the active piezo buzzer

Active piezo buzzer pin marked with (+) sign should be connected to Arduino Uno board digital pin which sends a signal, the other pin of active piezo buzzer should be connected to Arduino Uno board GND.

Signals and connections of the PS2 controller

Don’t trust the color scheme on the wiring picture. It can vary! Instead, use a voltmeter and test continuity between pin positions in the connector and the bare wires. A few people report odd color schemes, and a few not realize what end of the connector they were looking at and got the wiring completely backwards. A quick check is to power up the controller, and pushing the ‘Analog’ button. Either the LED should have been on when it powered up, or it should light up when you press the button. If not, the controller isn’t getting power and you probably have the wiring reversed.

VDD- power supply +3.3VDC. Connected to Arduino board 3.3V pin.

GND - ground. Connected to Arduino board GND pin.

ACK - Acknowledge. Acknowledge signal from Controller to PS2. This normally high line drops low about 12us after each byte for half a clock cycle, but not after the last bit in a set. This is a open collector output and requires a pull-up resistor (1 to 10k, maybe more).

PS2 - NC. Not in use

CLK - SPI communication. SCK (Serial Clock) - The clock pulses which synchronize data transmission generated by the master

CS - (ATT) Attention - used to get the attention of the controller. This line must be pulled low before each group of bytes is sent / received, and then set high again afterwards. This pin consider as “Chip Select” or “Slave Select” line that is used to address different controllers on the same bus.Chip select input pin (“Low” enable). 

NC - Not in use

D0 - Command. PS2 > Controller. Master In Slave Out (MISO) - SPI communication. The Slave line for sending data to the master. 

DI - Data. Controller > PS2. Master Out/Slave In (MOSI). SPI communication. The Master line for sending data to the peripherals. 

Build the circuit

Here we have interfaced the PS2 Wireless Controller with an Arduino. Upon each button press the Arduino receives the RF signal on the PS2 receiver and displays the it on the alphanumeric LCD module. We followed the standard PS2 protocol for realizing the communication algorithm, identical to the SPI protocol.

The PS2 receiver CLK line and ATT lines are held normally high. The  ATT operates like the Slave Select line under SPI. You pull it low to tell the controller you are talking to it and then send it back high once a communications cycle is complete. CMD is the data line to the controller and DATA is the data coming from the controller. Here in our project we are not using the acknowledge pin.

The following picture shows the needed connections with the Arduino Uno


Step by Step instruction

The "blank" sketch won't work for "native USB" based Arduinos like the Leonardo, Micro, Zero, etc! Use the

Leo_passthru sketch instead!
  1. Assemble the 4WD robot car chassis (base). See How to build the 4WD smart robot car Part 1 (STEPS from 1 to 19).
  2. Attach L298N H-Bridge motor driver module to lower part of robot chassis using two medium size and two small size bolts.
  3. We will make four DC motors configuration with speed control. The L298N H-Bridge motor driver module logic and all DC motors will be powered from 7.2V power supply. 
  4. Set the 5V_EN (5V) regulator jumper, remove jumpers on ENA and +5V, ENB and +5V on the motor driver board. You can check the motor polarity by connecting it to the battery pack.If it rotates in forward direction (red wire with positive and black wire with negative terminal of the battery) then the connection is ok). Do not forget that you need to use PWM pin for speed control (can be 3,5,6,9,10,11) - ENA, ENB should be connected to PWM pins.
  5. Attach 6 jumper cables F-M to ENA, IN1, IN2, IN3, IN4, ENB pins of motor driver board.
  6. Attach to power terminal of motor driver module 1 pc of positive jumper cable M-M (male to male) and 2 pcs of negative (GND) jumper cable M-M (male to male
  7. Attach the top of robot chassis with four medium size bolts. Put the jumper cables out.
  8. Attach Arduino Uno board to top part of robot chassis using two copper collars, two medium size bolts and two small size bolts.
  9. Attach violet jumper cable (ENA) to Arduino Uno digital pin 9, yellow jumper cable (ENB) to Arduino Uno digital pin 6, blue jumper cable (IN1) to Arduino Uno digital pin 7, orange jumper cable (IN2) to Arduino Uno digital pin 3, yellow jumper cable (IN3) to Arduino Uno digital pin 5, green jumper cable (IN4) to Arduino Uno digital pin 4, one negative (GND) jumper cable from motor driver board to Arduino Uno GND pin.
  10. Prepare the power supply for motor driver board. Attach power switch to it.
  11. Attach power supply with tape or two bolts and two nuts to the top of robot chassis and connect to the power supply jumper cables.
  12. There are several options for connecting the controller to the Arduino: a) to cut the connector off and connect the wires into a row of terminal headers or directly solder them to some other plug or jumper cables; b) to retain the plug and find a suitable socket .
  13. Uncrew the PS2 receiver. Unsolder the male connector and solder 7 M-M jumper cables.
  14. Do wiring for Wireless PS2 controller on medium size board. You will need Wireless PS2 controller, four resistors, and jumper cables M-M. See on the picture below.
  15. Attach medium size breadboard to the top part of robot chassis. Connect jumper cables to Arduino Uno board.
  16. Open Arduino IDE on your PC/laptop.
  17. Plug your Adruino Uno board into your PC/laptop and select the correct board and com port.
  18. Attach 9V battery with 9V Battery Clip to the top of robot chassis but do not connect it to Arduino Jack.
  19. Verify and upload sketch to your Adruino Uno board.
  20. Unplug your Adruino Uno board from your PC/laptop.
  21. Connect 9V battery with 9V Battery Clip and Barrel Jack to Arduino Jack.
  22. Turn the power switch (red switch) ON.
  23. Power on the PS2 controller - set the power switch to ON position. Hold down the handle for PS2 button so that the RX light flashes into a light that has been connected successfully.
  24. You Wireless PS2 controller controlled 4WD smart robot car is ready. Congrats!!!
  25. We added forward lights, back lights, buzzer, turn left and turn right lights. You need 6 LEDs, 1 Active Piezo buzzer, 6 resistors 220 Ohm and 14 jumper cables M-M. Do final wiring with Arduino Uno board.
  26. Congrats. That's it.
  27. Test the robot is running correctly or not. If motor turn on wrong direction - interchange power o/p of motor drive module. If sleep L298N motor drive module - check GND connection between Arduino controller and motor drive module.

Code

To use the library in an Arduino Sketch, it needs to be called using the following commands;

#include

PS2X ps2x; 

In the setup part of the sketch the command to let the Arduino know how the controller is connected to it, looks like;

ps2x.config_gamepad(13,12,10,11, false, false);  

where the numbers are the Arduino's digital pins where the Playstation 2 Controller pins are connected as below;

gamepad(clock, command, attention, data, pressure sensitivity enabled, rumble enabled)

If you wanted to use the button pressure sensitivity feature, in the command line above you would set this to “true” and if you wanted the rumble motor available to provide feedback then you would set this to “true” too. For use of the rumble feature please look at the example sketch that comes with the PS2X library.

Once the controller is set up, the Arduino loops through the sketch continuously. Once per loop through, the Arduino needs to communicate with the controller to gather all input data. This is done with the following command.

ps2x.read_gamepad();  

Now we can read which control has been used. The labels used in the PS2X library are very logical. The diagram below shows each of the names for the buttons and sticks.

The buttons with the coloured shapes can also be referred to by the names PSB_TRIANGLE, PSB_CIRCLE, PSB_CROSS, and PSB_SQUARE.

To use the analogue pressure sensitivity on the keys the names are the same except for substituting “PSAB” for “PSB”. So to allow a pressure reading from the Green Triangle button the name would be PSAB_GREEN or PSAB_TRIANGLE.

The buttons can be pressed, pressed and held, or pressed with a varying pressure, so there are a number of methods that can be applied to the buttons and joystick. The methods are; Button Pressed, Button, and Analog. Here are some examples of how these are used.

ps2x.ButtonPressed(PSB_RED) is for a simple press of the red circle button.

ps2x.Button(PSB_PAD_DOWN) is for the down button on the pad being pressed and held.

ps2x.Analog(PSAB_CROSS) is the command for measuring the pressure applied to the “X” button, if pressure sensitivity has been enabled. As you can see the “PSAB” form of the name has been used for the button.

ps2x.Analog(PSS_RY) is the command to obtaining readings off the right analogue stick in the vertical direction.

Summary

We learnt how to connect different components and make wireless PS2 controller controlled 4WD smart robot car. 

Library

  • All libraries attached on the begining of the project description
  • PS2X_lib library. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about it here.

Sketch

  • See attachments on the begining of this project


Other projects of Lex C.

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 01-04-2018
Viewed: 1997 times