I found this old toy car in my room, a good looking one, with a suspension system on all of its wheels. It came with its own radio system: a customized PCB capable of 2.4Ghz of radiofrequency. With that said, the chance of me picking up and playing this toy again was slim. Therefore, I decided to give it a renovation.
I used the ESP8266 wifi module and the L298N motor driver, both of which were easy to implement. First, I cut out the original PCB that was attached to its motors and battery and screwed the motor wires to the driver. Then, I went to my IDE and created PIN connections with all of the motors.
int In1 = 0; int In2 = 2; int ENA = 4; int In3 = 13; int In4 = 12; int ENAB = 14; void setup() { Serial.begin(57600); pinMode(In1,OUTPUT); pinMode(In2,OUTPUT); pinMode(ENA, OUTPUT); pinMode(In3,OUTPUT); pinMode(In4,OUTPUT); pinMode(ENAB, OUTPUT); }
This is where it gets interesting. The L298N is constructed like an H-bridge. In1~In4 are gates, and I can control them through digital signals. Then, by sending digital signals through ENA or ENAB, current goes through this bridge according to the route that I have configured with the gates, creating different patterns of rotation for the motors. Howtomechatronics has a great explanation on this motor driver.
Here is an example of how I controlled the motors:
digitalWrite(In1,HIGH); digitalWrite(In2, LOW); digitalWrite(ENA, HIGH);
After configuring pins for the motors, I created a simple HTML page on my local host to test and control the motors:
What it essentially does is that, everytime a button is pressed, the HTTP request's URL is added with either "/right", "/left", "/back", or "/front". And, the program searches that term on the request.
The completed code can be downloaded from here.
As I was more comfortable coding in Java, I created a Java program that sends HTTP requests to the ESP8266 board. Originally, I pined the localhost by using the URL, but after some digging, I realized that I needed to make an actual HTTP request.
private static void test(String input){ try{ String url = "http://192.168.1.20/" + input; URL request_url = new URL(url); HttpURLConnection http_conn = (HttpURLConnection)request_url.openConnection(); http_conn.setConnectTimeout(100000); http_conn.setReadTimeout(100000); http_conn.setInstanceFollowRedirects(true); System.out.println(String.valueOf(http_conn.getResponseCode())); }catch(Exception e){ e.printStackTrace(); } }
If the http_conn.getResponseCode() is 200, then I know the request is completed.
Though this car is now Wifi and Bluetooth compatible, I have yet to decide what to build with it. If you have any ideas, please leave it in the comment!