Uno car with L293D driver and XD-YK04 receiver

Hi, I’m trying to have the Uno run the L293D driver on signals from the XD-YK04. I think I have it wired correctly but can’t get a sketch to work.

Uno and the motor shield are connected on pins 3, 4, 5, 6, 7, 8, 11, 12, and on the other side VIN, GND, GND, 5V, 3.3V, and RES.

The receiver is connected to Uno’s 5V, GND, A0, A1, A2, and A3 pins.

I’m using the “Simple Motor Shield sketch” and I tried to insert A1 as a signal to drive. int drivePin = A1…pinMode(drivePin, INPUT)

Right now the motor runs forward at full speed, pauses, then backward at half speed, unresponsive to signals from the remote control.

Thanks in advance for any help!

// Simple Motor Shield sketch
// -----------------------------------
//
// By arduino.cc user "Krodal".
// June 2012
// Open Source / Public Domain
//
// Using Arduino 1.0.1
//
// A simple sketch for the motor shield,
// without using the Adafruit library.
//



#include <Servo.h>


// Arduino pins for the shift register
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8

// 8-bit bus after the 74HC595 shift register
// (not Arduino pins)
// These are used to set the direction of the bridge driver.
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6

// Arduino pins for the PWM signals.
#define MOTOR1_PWM 11
#define MOTOR2_PWM 3
#define MOTOR3_PWM 6
#define MOTOR4_PWM 5
#define SERVO1_PWM 10
#define SERVO2_PWM 9

// Codes for the motor function.
#define FORWARD 1
#define BACKWARD 2
#define BRAKE 3
#define RELEASE 4

int drivePin = A1;

// Declare classes for Servo connectors of the MotorShield.
Servo servo_1;
Servo servo_2;


void setup()
{
  Serial.begin(9600);
  Serial.println("Simple Motor Shield sketch");
  pinMode(drivePin, INPUT);

  // Use the default "Servo" library of Arduino.
  // Attach the pin number to the servo library.
  // This might also set the servo in the middle position.
  servo_1.attach(SERVO1_PWM);
  servo_2.attach(SERVO2_PWM);
}


void loop()
{
if(digitalRead(drivePin) == HIGH)
  
  // Suppose there are two servo motors connected.
  // Let them move 180 degrees.
  servo_1.write(0);  
  delay(1000);
  servo_1.write(180);
  delay(2000);

  servo_2.write(0);
  delay(1000);
  servo_2.write(180);
  delay(2000);



  motor_output(MOTOR3_A, HIGH, 255);
  delay(2000);
  motor_output(MOTOR3_A, LOW, 255);


  // Suppose a DC motor is connected to M1_A(+) and M1_B(-)
  // Let it run full speed forward and half speed backward.
  // If 'BRAKE' or 'RELEASE' is used, the 'speed' parameter
  // is ignored.
  motor(1, FORWARD, 255);
  delay(2000);
  // Be friendly to the motor: stop it before reverse.
  motor(1, RELEASE, 0);
  delay(500);
  motor(1, BACKWARD, 128);
  delay(2000);
  motor(1, RELEASE, 0);
}






void motor(int nMotor, int command, int speed)
{
  int motorA, motorB;

  if (nMotor >= 1 && nMotor <= 4)
  {  
    switch (nMotor)
    {
    case 1:
      motorA   = MOTOR1_A;
      motorB   = MOTOR1_B;
      break;
    case 2:
      motorA   = MOTOR2_A;
      motorB   = MOTOR2_B;
      break;
    case 3:
      motorA   = MOTOR3_A;
      motorB   = MOTOR3_B;
      break;
    case 4:
      motorA   = MOTOR4_A;
      motorB   = MOTOR4_B;
      break;
    default:
      break;
    }

    switch (command)
    {
    case FORWARD:
      motor_output (motorA, HIGH, speed);
      motor_output (motorB, LOW, -1);     // -1: no PWM set
      break;
    case BACKWARD:
      motor_output (motorA, LOW, speed);
      motor_output (motorB, HIGH, -1);    // -1: no PWM set
      break;
    case BRAKE:

      motor_output (motorA, LOW, 255); // 255: fully on.
      motor_output (motorB, LOW, -1);  // -1: no PWM set
      break;
    case RELEASE:
      motor_output (motorA, LOW, 0);  // 0: output floating.
      motor_output (motorB, LOW, -1); // -1: no PWM set
      break;
    default:
      break;
    }
  }
}



void motor_output (int output, int high_low, int speed)
{
  int motorPWM;

  switch (output)
  {
  case MOTOR1_A:
  case MOTOR1_B:
    motorPWM = MOTOR1_PWM;
    break;
  case MOTOR2_A:
  case MOTOR2_B:
    motorPWM = MOTOR2_PWM;
    break;
  case MOTOR3_A:
  case MOTOR3_B:
    motorPWM = MOTOR3_PWM;
    break;
  case MOTOR4_A:
  case MOTOR4_B:
    motorPWM = MOTOR4_PWM;
    break;
  default:
    // Use speed as error flag, -3333 = invalid output.
    speed = -3333;
    break;
  }

  if (speed != -3333)
  {
    // Set the direction with the shift register
    // on the MotorShield, even if the speed = -1.
    // In that case the direction will be set, but
    // not the PWM.
    shiftWrite(output, high_low);

    // set PWM only if it is valid
    if (speed >= 0 && speed <= 255)    
    {
      analogWrite(motorPWM, speed);
    }
  }
}



void shiftWrite(int output, int high_low)
{
  static int latch_copy;
  static int shift_register_initialized = false;

  // Do the initialization on the fly,
  // at the first time it is used.
  if (!shift_register_initialized)
  {
    // Set pins for shift register to output
    pinMode(MOTORLATCH, OUTPUT);
    pinMode(MOTORENABLE, OUTPUT);
    pinMode(MOTORDATA, OUTPUT);
    pinMode(MOTORCLK, OUTPUT);

    // Set pins for shift register to default value (low);
    digitalWrite(MOTORDATA, LOW);
    digitalWrite(MOTORLATCH, LOW);
    digitalWrite(MOTORCLK, LOW);
    // Enable the shift register, set Enable pin Low.
    digitalWrite(MOTORENABLE, LOW);

    // start with all outputs (of the shift register) low
    latch_copy = 0;

    shift_register_initialized = true;
  }

  // The defines HIGH and LOW are 1 and 0.
  // So this is valid.
  bitWrite(latch_copy, output, high_low);

  // Use the default Arduino 'shiftOut()' function to
  // shift the bits with the MOTORCLK as clock pulse.
  // The 74HC595 shiftregister wants the MSB first.
  // After that, generate a latch pulse with MOTORLATCH.
  shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
  delayMicroseconds(5);    // For safety, not really needed.
  digitalWrite(MOTORLATCH, HIGH);
  delayMicroseconds(5);    // For safety, not really needed.
  digitalWrite(MOTORLATCH, LOW);
}

I haven’t looked too deeply at your logic, but you need { and } with an “if”, if it has more than one line that’s to be run conditionally.

Without {, only the next line is if-ed, and the next lines run always.

You have this:

void loop()
{
  if (digitalRead(drivePin) == HIGH)
// <<<<<<<<<<<<<< no {
    // Suppose there are two servo motors connected.
    // Let them move 180 degrees.
    servo_1.write(0);  // <<<<<<<<<<<<, only this line is if-ed
  delay(1000);      //<<<<<<<<<<<<<<from here they all run always
  servo_1.write(180);
  delay(2000);

  servo_2.write(0);
  delay(1000);

You need this:

void loop()
{
  if (digitalRead(drivePin) == HIGH)
{  <<<<<<<<<<<<<< new { here
    // Suppose there are two servo motors connected.
    // Let them move 180 degrees.
    servo_1.write(0);  //<<<<<< all lines from here to the closing } are if-ed
  delay(1000);      
  servo_1.write(180);
  delay(2000);

  servo_2.write(0);
  delay(1000);

} //<<<<<<<<<<<<< remember to close it down here somewhere

edit: And btw nobody knows what motor shield you have, just that it has a 293. Supply a link to it, and while you’re at it, a link to the remote. Also describing connections in words doesn’t work too well: diagrams are better.

Thanks man. Yeah, that'll make a big difference. :)

The driver is here: https://www.sainsmart.com/products/l293d-motor-drive-shield-for-arduino Manual for the driver is here: http://s3.amazonaws.com/s3.image.smart/download/101-50-115/L293.rar

And the remote is here: https://www.adafruit.com/product/1096

I can't open a rar. Never assume everyone on a forum knows what you do, or has the same utilities to do stuff that you can do.

I couldn’t open a .rar either, but that was the only datasheet I could find.

Downloaded a program to unzip it, here it is.

l293D.pdf (494 KB)

BTW, I know that this is a lot of code and it has signals for servos in it, but I’m just trying to get the two dc motors to work, not the servos.

So, briefly, the question is how to code one signal from the RC to make the motors drive forward, one to make the them drive in reverse, one to make them turn right, and one to make them turn left.

Any help would be highly appreciated - thanks again!

Problem solved. For the next unfortunate person who googles this combination of terms, here’s my code that works for controlling an Arduino with the L293D driver using the XD-YK04 receiver.

Uno and the motor shield are connected on pins 3, 4, 5, 6, 7, 8, 11, 12, and on the other side VIN, GND, GND, 5V, 3.3V, and RES.

The receiver is connected to Uno’s 5V, GND, A0, A1, A2, and A3 pins.

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);

int drivePin0 = A0;
int drivePin1 = A1;
int drivePin2 = A2;
int drivePin3 = A3;

void setup(){
  Serial.begin(9600);
  Serial.println("Motor test!");

  motor1.setSpeed(200);
  motor2.setSpeed(200);
}

void loop(){

if(digitalRead(drivePin0) == HIGH){

  motor1.run(FORWARD);,
  |
  motor2.run(FORWARD);
  delay(1000);
  
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  delay(1000);
}
  
if(digitalRead(drivePin1) == HIGH){

  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  delay(1000);

  motor1.run(RELEASE);
  motor2.run(RELEASE);
  delay(1000);
}

if(digitalRead(drivePin2) == HIGH){

  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  delay(1000);

  motor1.run(RELEASE);
  motor2.run(RELEASE);
  delay(1000);
}


if(digitalRead(drivePin3) == HIGH){

  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  delay(1000);

  motor1.run(RELEASE);
  motor2.run(RELEASE);
  delay(1000);
}
}
  motor1.run(FORWARD);,
  |
  motor2.run(FORWARD);

WTF? What is that , there for? What is the | there for?