Are you scratching your head wondering why your 2WD car project isn’t moving as your Arduino code commands? You’ve meticulously written the program, but the wheels just won’t turn the way you intended. This is a common frustration for beginners diving into the world of robotics and Arduino car projects. Let’s break down a sample Arduino code snippet designed to control a 2WD car and understand how it should make your car respond to your commands.
This article will explore a basic Arduino sketch that utilizes serial communication to dictate the movements of a two-wheel drive car. We’ll dissect the code, line by line, to clarify how each part contributes to controlling your car’s motors, and potentially help you pinpoint why your “2wd Car Wont Do The Coding I Put In It.”
Understanding the Code: 2WD Car Control with Arduino
The provided code is a foundational example for controlling a 2WD car using an Arduino board and serial communication. It’s designed to receive simple commands via the serial monitor and translate them into motor actions, making your car move forward, backward, left, right, or stop. Let’s delve into each section:
Variable Declarations: Setting up the Stage
int motorPin1 = 7;
int motorPin2 = 8;
int enablePin_1 = 6;
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int buzzer = 11;
int f_b_light = 12 ;
int state;
int flag=0; //makes sure that the serial only prints once the state
boolean buzzerstatus = 0;
boolean LEDstatus = 0;
This initial block declares the integer variables that will represent the Arduino pins connected to your car’s components.
motorPin1
,motorPin2
,motorPin3
,motorPin4
: These pins will control the direction of rotation for your DC motors. Typically, for a 2WD car, you’ll have two motors. Pins 1 & 2 might control one motor, and pins 3 & 4 the other.enablePin_1
,enablePin_2
: Enable pins are often used with motor drivers (like the L293D, though not explicitly stated in the code, it’s implied). These pins allow you to turn the power to the motors on and off, or control their speed (with PWM, which isn’t used here, it’s a simple HIGH/LOW control).buzzer = 11
: Pin for a buzzer. This code includes commands to potentially activate a buzzer, though its function isn’t elaborated upon in the basic movement logic.f_b_light = 12
: Pin for a forward/backward light. Similar to the buzzer, this suggests the code can control a light, possibly to indicate the car’s direction.state
: An integer variable to store the command received via serial communication. This variable determines the car’s action (stop, forward, etc.).flag = 0
: A flag variable used to ensure that serial print statements (for the LCD) are executed only once per state change, preventing repetitive printing.buzzerstatus
,LEDstatus
: Boolean variables, likely intended for tracking the status of the buzzer and LED, although they are not actively used in the core movement logic of this particular code snippet.
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 10, 9, A0, A1, A2);
These lines include the LiquidCrystal.h
library, essential for using an LCD display. The line LiquidCrystal lcd(13, 10, 9, A0, A1, A2);
initializes an LCD object named lcd
. The numbers in the parentheses (13, 10, 9, A0, A1, A2) are the Arduino pins connected to the LCD’s control and data pins. Note: Pin 13 is also defined as OUTPUT in setup()
, which might create a conflict if also used for LCD. It is important to double-check your wiring against your code.
setup()
Function: Initial Configuration
void setup() {
// sets the pins as outputs:
lcd.begin(16, 2);
pinMode(buzzer,OUTPUT);
pinMode(f_b_light,OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin_1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin_2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin_1, HIGH);
digitalWrite(enablePin_2, HIGH);
pinMode(13,OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
The setup()
function runs once at the beginning of your Arduino program. It’s used to initialize settings:
lcd.begin(16, 2);
: Initializes the LCD to 16 columns and 2 rows.pinMode(..., OUTPUT);
: Configures various pins as OUTPUT. This is crucial because you are sending signals from the Arduino to the motors, buzzer, and light. You’re telling the Arduino to control these components.digitalWrite(enablePin_1, HIGH); digitalWrite(enablePin_2, HIGH);
: Sets the enable pins HIGH initially. This is generally done to allow the motor driver to function and enable motor movement. If these are LOW, the motors likely won’t turn, regardless of the signals to the motor pins.pinMode(13,OUTPUT);
: Sets digital pin 13 as OUTPUT. As mentioned before, if pin 13 is also used for the LCD, this line might be redundant or conflicting. Verify your LCD wiring.Serial.begin(9600);
: Starts serial communication at a baud rate of 9600. This allows your Arduino to receive commands from your computer via the serial monitor.
loop()
Function: Continuous Control
void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is '0' the DC motor will turn off
if (state == '0') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("STOP");
flag=1;
}
}
// if the state is '1' the motor will forward
else if (state == '1') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("FORWARD");
flag=1;
}
}
// if the state is '2' the motor will backward
else if (state == '2') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("REVERSE");
flag=1;
}
}
else if (state == '3') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("RIGHT");
flag=1; //Left wheel forward, Right wheel static
flag=1;
}
} else if (state == '4') {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
if(flag == 0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current State:");
lcd.setCursor(0, 1);
lcd.print("LEFT");
flag=1;
}
}
}
The loop()
function runs continuously after setup()
. This is where the main control logic resides:
-
if(Serial.available() > 0)
: This line checks if there is any data available in the serial buffer. If you send data from the serial monitor, this condition becomes true. -
state = Serial.read();
: Reads the first byte of serial data and stores it in thestate
variable. The code expects you to send characters ‘0’, ‘1’, ‘2’, ‘3’, or ‘4’ via the serial monitor. -
flag = 0;
: Resets theflag
variable to allow LCD updates for the new state. -
if (state == '0')
,else if (state == '1')
, etc.: A series ofif/else if
statements check the value of thestate
variable. Based on the state, different sets ofdigitalWrite()
commands are executed to control the motors.- State ‘0’ (STOP): All motor pins are set LOW, effectively stopping both motors.
- State ‘1’ (FORWARD):
motorPin2
andmotorPin4
are set HIGH, whilemotorPin1
andmotorPin3
are LOW. This configuration is intended to make both motors rotate in a forward direction. Important: Motor direction depends heavily on your wiring to the motor driver and the motors themselves. You may need to swap HIGH/LOW or motor pins to achieve actual forward motion. - State ‘2’ (REVERSE):
motorPin1
andmotorPin3
are set HIGH, andmotorPin2
andmotorPin4
are LOW, intended for reverse motion. Again, wiring is critical. - State ‘3’ (RIGHT):
motorPin2
is HIGH, and all others are LOW. This aims to turn RIGHT by making only one side’s motor move forward (likely the left wheel if pins 1&2 are on the left motor and 3&4 on the right). The comment//Left wheel forward, Right wheel static
confirms this intention. - State ‘4’ (LEFT):
motorPin4
is HIGH, and the rest are LOW, aiming for a LEFT turn by moving the right wheel forward (if pins 3&4 control the right motor) and keeping the left wheel static.
-
LCD Display Updates: Within each state’s
if
block, there’s a conditional blockif(flag == 0)
. This ensures the LCD is updated with the “Current State:” and the corresponding action (STOP, FORWARD, etc.) only when thestate
changes. This prevents the LCD from being constantly rewritten with the same information, reducing flicker and unnecessary writing.
Troubleshooting: Why Your 2WD Car Might Not Respond
If your 2WD car isn’t responding to this code, here’s a checklist of potential issues to investigate:
-
Wiring Errors: Double and triple-check your wiring! Incorrect wiring is the most common culprit.
- Motor Pins: Ensure
motorPin1
,motorPin2
,motorPin3
,motorPin4
are connected correctly to your motor driver (like L293D) and then to your motors. Refer to your motor driver datasheet for correct pin connections. - Enable Pins: Verify
enablePin_1
andenablePin_2
are connected to the enable input of your motor driver. - Power Supply: Make sure your motors and Arduino are getting sufficient power. Motors often require a separate power supply from the Arduino’s USB power.
- LCD Wiring: If your LCD is not displaying anything, check the LCD pin connections to the Arduino pins defined in
LiquidCrystal lcd(...)
. Also, ensure you have the LCD library correctly included.
- Motor Pins: Ensure
-
Motor Driver Issues:
- Motor Driver Type: The code implicitly assumes a motor driver. If you’re not using one, or using an incompatible one, the motors won’t work. L293D is a common choice for DC motor control with Arduino.
- Motor Driver Enable: Ensure the enable pins of your motor driver are correctly controlled (HIGH to enable, often).
- Motor Driver Power: Motor drivers also need power. Verify it’s connected and sufficient for your motors.
-
Serial Communication Problems:
- Serial Monitor Baud Rate: In the Arduino IDE’s serial monitor, ensure the baud rate is set to 9600 to match
Serial.begin(9600);
in your code. - No Serial Data Sent: Are you actually sending characters ‘0’, ‘1’, ‘2’, ‘3’, or ‘4’ through the serial monitor and pressing “Send”?
- Incorrect Characters: Make sure you are sending the correct characters. Sending “1” is different from sending the integer 1. The code reads characters.
- Serial Monitor Baud Rate: In the Arduino IDE’s serial monitor, ensure the baud rate is set to 9600 to match
-
Code Errors:
- Pin Conflicts: As noted, using pin 13 for both LCD and as a general output might cause issues. Resolve any pin conflicts.
- Logic Errors: Carefully review the motor control logic in each
if/else if
block. Understand which pins are going HIGH and LOW for each state and how that should translate to motor direction based on your wiring and motor driver. - Library Issues: Ensure the
LiquidCrystal.h
library is correctly installed in your Arduino IDE if you are using the LCD.
-
Motor and Hardware Problems:
- Motor Functionality: Test your motors independently to ensure they are working.
- Wheel Obstructions: Make sure nothing is physically blocking the wheels from turning.
Conclusion: Getting Your 2WD Car Moving
This code provides a basic framework for controlling a 2WD car with Arduino and serial commands. By understanding each part of the code and systematically troubleshooting potential issues with wiring, hardware, serial communication, and code logic, you should be able to get your robot car responding to your commands. Remember to always double-check your connections and take it step by step! With careful attention to detail, you can overcome the frustration of a “2wd car wont do the coding i put in it” and successfully bring your project to life.