This tutorial instructs you on how to control a robot car wirelessly using an Arduino from a web browser on your smartphone or PC via WiFi. The control is managed through a graphical web user interface that utilizes WebSocket technology, enabling smooth and dynamic operation of the car.
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you. Additionally, some links direct to products from our own brand, DIYables .
About 2WD RC Car and WebSocket
Now, why choose WebSocket? Here are the reasons:
Without WebSocket, you would have to reload the page every time you want to change the car’s direction. This is not efficient!
With WebSocket, a special connection is established between the webpage and the Arduino. This allows you to send commands to the Arduino in the background, without the need to reload the page. The outcome? The robot car responds smoothly and instantly. Quite impressive, right?
In summary, WebSocket facilitates the smooth, real-time control of the robot.
We have specific tutorials about 2WD RC Car and WebSocket. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to Arduino, Arduino code... Learn more about them at the following links:
The Arduino code sets up both a web server and a WebSocket server. Here’s how it functions:
When you type the Arduino's IP address into a web browser, it sends a request for the webpage (User Interface) from the Arduino.
The Arduino’s web server replies by delivering the content of the webpage (HTML, CSS, JavaScript).
Your web browser then displays this webpage.
Inside the webpage, the JavaScript code initiates a WebSocket connection with the WebSocket server on the Arduino.
Once this WebSocket connection is active, any button presses or releases on the webpage cause the JavaScript code to discreetly send commands to the Arduino via this connection.
The WebSocket server on the Arduino receives these commands and directs the robot car accordingly.
Below is a table showing the list of commands that the webpage sends to the Arduino based on user actions:
User's Action
Button
Command
Car Action
PRESS
UP
1
MOVE FORWARD
PRESS
DOWN
2
MOVE BACKWARD
PRESS
LEFT
4
TURN LEFT
PRESS
RIGHT
8
TURN RIGHT
PRESS
STOP
0
STOP
RELEASE
UP
0
STOP
RELEASE
DOWN
0
STOP
RELEASE
LEFT
0
STOP
RELEASE
RIGHT
0
STOP
RELEASE
STOP
0
STOP
Wiring Diagram between 2WD RC Car and Arduino
This image is created using Fritzing. Click to enlarge image
Normally, you would need two separate power sources:
One for the motor.
Another for both the Arduino board and the L298N module (which serves as the motor driver).
However, you can simplify this setup by using just one power source – four 1.5V batteries to make a total of 6V. Here's how to do it:
Connect the batteries to the L298N module as directed.
Remove the two jumpers from the ENA and ENB pins to the 5 volts on the L298N module.
Insert a jumper labeled 5VEN (indicated by a yellow circle on the diagram).
Connect the 12V pin on the L298N module to the Vin pin on the Arduino. This connection will power the Arduino directly from the batteries.
The 2WD RC car includes an on/off switch, providing you with the option to connect the battery through the switch. This setup allows you to turn the power for the car on and off as needed. If you prefer a simpler arrangement, you can choose to bypass the switch altogether.
Arduino Code
The webpage's content (HTML, CSS, JavaScript) are stored separately on an index.h file. So, we will have two code files on Arduino IDE:
An .ino file that is Arduino code, which creates a web sever and WebSocket Server, and controls car
Now you have the code in two files: ArduinoGetStarted.com.ino and index.h
Click Upload button on Arduino IDE to upload code to Arduino.
Open the Serial Monitor
Check out the result on Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Arduino Uno R4 WiFi - WebSocket Server
Connected! IP Address: 192.168.0.254
SSID: YOUR_WIFI_SSID
IP Address: 192.168.0.254
Signal strength (RSSI): -44 dBm
WebSocket server started on port 81
WebSocket URL: ws://192.168.0.254:81
WebSocket server enabled successfully
Ln 11, Col 1
Arduino Uno on COM15
2
Take note of the IP address displayed, and enter this address into the address bar of a web browser on your smartphone or PC.
You will see the webpage it as below:
Click the CONNECT button to connect the webpage to Arduino via WebSocket.
Now you can control the car to turn left/right, move forward/backward via the web interface.
To save the memory of Arduino, the images of the control buttons are NOT stored on Arduino. Instead, they are stored on the internet, so, your phone or PC need to have internet connection to load images for the web control page.
※ NOTE THAT:
If you modify the HTML content in the index.h and does not touch anything in ArduinoGetStarted.com.ino file, when you compile and upload code to Arduino, Arduino IDE will not update the HTML content.
To make Arduino IDE update the HTML content in this case, make a change in the ArduinoGetStarted.com.ino file (e.g. adding empty line, add a comment....)
Line-by-line Code Explanation
The above Arduino code contains line-by-line explanation. Please read the comments in the code!
You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!