Joy Stick DC Motor Control

Hello all

My project uses two Arduinos, one with a pair of joysticks and a 433Mhz transmitter and a receiver and two DC motors controlled by an L293D IC on the other.

The code is simple, the data transfer part was perfected by jremmington last week. And now, as it stands, the joystick 1 and 2, X and Y data is transferred to the second Arduino and displays ok on the serial monitor. When the joysticks are moved, the displayed data adjusts as expected.

My problem is that only motor no.1 works and I have to rem out the code for motor no.2 for that to happen! The Y axis centre point values of the two joysticks are used for zero motor speed. Greater than 512 is 0 - 255 speed clockwise and less than 512 is used for 0 - 255 reverse speed. (They're not actually exactly 512 but you get what I mean)

If I upload the code for motor no.2 then all four values display as 0 and motor no.1 runs backwards at full speed.

If I flip the motor control chip the symptoms are the same, so the hard ware is connected correctly. I've triple checked the code and re-written it from scratch but the symptom persists!

The first version used some y = M x + C maths to calculate the 8 bit motor speed value, but that version only had one motor and it worked. The current version uses the Map() function. Is there a quirk of the Map() function that I need to be aware of or have I typed a schoolboy error into my code?

I would be grateful if someone could shed some light please.

Graeme

/*
 *
 *  The Bait Boat 
 *  Rev 03_02_Receiver    - Receiver Controlling Two DC motors
 *  25/04/24
 *  
 *  Description
 *  Prototype to test the Transmitter/Receiver Pair. Joystick controlling the Transmitter and Receiver controlling an L293D IC and DC Motors
 *  
 *  Rev History
 *  Rev 01.01             - DC Motor control
 *  Rev 02.01_Transmitter - With text message string
 *  Rev 02.02_Transmitter - Transmitter string text message 
 *  Rev 02.02_Receiver    - Receiver string text message 
 *  Rev 02.03_Transmitter - Transmitter with Joystick position string
 *  Rev 02.03_Receiver    - Receiver
 *  Rev 02.04_Transmitter - Transmitter with Dual Joysticks and 2 x 4 character string
 *  Rev 02.04_Receiver    - Receiver       
 *  Rev 02.05_Transmitter - jremington
 *  Rev 02.05_Receiver    - jremington
 *  Rev 03_01_Transmitter - Transmitter Controlled by Joystick 
 *  Rev 03_01_Receiver    - Receiver Controlling a DC motor 
 *  Rev 03_02_Transmitter - Transmitter Controlled by Two Joysticks 
 *  Rev 03_02_Receiver    - Receiver Controlling Two DC motors  
 *
 */
 
//  Libraries
#include <RadioHead.h>
#include <RH_ASK.h>
#include <SPI.h>

//  Variables
int JoyStick1XVal;
int JoyStick1YVal;
int JoyStick2XVal;
int JoyStick2YVal;
int JoyStick[4];    
int Speed1Pin = 5;      //  Motor No1 Left - EN1(1)
int Direction1Pin1 = 4; //  Motor Black - IN1(2)
int Direction1Pin2 = 3; //  Motor Red - IN2(7)
int Motor1Speed;
int Speed2Pin = 9;      //  Motor No2 Right - EN2(9)
int Direction2Pin1 = 8; //  Motor Black - IN3(10)
int Direction2Pin2 = 7; //  Motor Red - IN4(15)
int Motor2Speed;
int LoopDelay = 200;

//  Create RH Driver
RH_ASK rf_driver;


void setup() 
{
  Serial.begin (9600);

  rf_driver.init(); //  Arduino Pin 11

  pinMode (Speed1Pin, OUTPUT);
  pinMode (Direction1Pin1, OUTPUT);
  pinMode (Direction1Pin2, OUTPUT);
  pinMode (Speed2Pin, OUTPUT);
  pinMode (Direction2Pin1, OUTPUT);
  pinMode (Direction2Pin2, OUTPUT);
}


void loop() 
{

  uint8_t JoySticklen = sizeof JoyStick;
  
  if (rf_driver.recv((uint8_t *) JoyStick, &JoySticklen))
  {
    JoyStick1XVal = JoyStick[0];  
    JoyStick1YVal = JoyStick[1];  
    JoyStick2XVal = JoyStick[2];  
    JoyStick2YVal = JoyStick[3];  
  }

  if (JoyStick1YVal < 503)
  {
    digitalWrite (Direction1Pin1, LOW);             //  Pin1 High - Pin2 Low - Clockwise
    digitalWrite (Direction1Pin2, HIGH);            //  Pin2 High - Pin1 Low - Anti Clockwise
    Motor1Speed = map(JoyStick1YVal,503,0,0,250);
    analogWrite (Speed1Pin,Motor1Speed);
  }

  if (JoyStick1YVal >= 503)
  {
    digitalWrite (Direction1Pin1, HIGH);            //  Pin1 High - Pin2 Low - Clockwise
    digitalWrite (Direction1Pin2, LOW);             //  Pin2 High - Pin1 Low - Anti Clockwise
    Motor1Speed = map(JoyStick1YVal,503,1023,0,250);
    analogWrite (Speed1Pin,Motor1Speed);
  }

/*
  if (JoyStick2YVal < 530)
  {
    digitalWrite (Direction2Pin1, HIGH);             //  Pin1 High - Pin2 Low - Clockwise
    digitalWrite (Direction2Pin2, LOW);            //  Pin2 High - Pin1 Low - Anti Clockwise
    Motor2Speed = map(JoyStick2YVal,530,0,0,250);
    analogWrite (Speed2Pin,Motor2Speed);
  }

  if (JoyStick2YVal >= 530)
  {
    digitalWrite (Direction2Pin1, LOW);            //  Pin1 High - Pin2 Low - Clockwise
    digitalWrite (Direction2Pin2, HIGH);             //  Pin2 High - Pin1 Low - Anti Clockwise
    Motor2Speed = map(JoyStick2YVal,530,1023,0,250);
    analogWrite (Speed2Pin,Motor2Speed);
  }
*/

  Serial.print(JoyStick1XVal); 
  Serial.print(" - "); 
  Serial.print(JoyStick1YVal);  
  Serial.print(" - ");
  Serial.print(JoyStick2XVal);  
  Serial.print(" - ");
  Serial.println(JoyStick2YVal);  
  
  delay(LoopDelay);
}



//  EoF

Try a bigger deadband. I think you have about +/-10. Try 50.

What do the Serial Monitor values look like (you can only copy from the visible Serial Monitor... if you want a larger capture, install PuTTY or minicom, close the IDE Serial Monitor and use the new app as your Serial Monitor).

Thanks for that.

I changed the code to:


  if (JoyStick1YVal < 475)
  {
    digitalWrite (Direction1Pin1, LOW);             //  Pin1 High - Pin2 Low - Clockwise
    digitalWrite (Direction1Pin2, HIGH);            //  Pin2 High - Pin1 Low - Anti Clockwise
    Motor1Speed = map(JoyStick1YVal,512,0,0,250);
    analogWrite (Speed1Pin,Motor1Speed);
  }

  if (JoyStick1YVal >= 475 && JoyStick1YVal <= 525)
  {
    Motor1Speed = 0;
    analogWrite (Speed1Pin,Motor1Speed);
  }

  if (JoyStick1YVal > 525)
  {
    digitalWrite (Direction1Pin1, HIGH);            //  Pin1 High - Pin2 Low - Clockwise
    digitalWrite (Direction1Pin2, LOW);             //  Pin2 High - Pin1 Low - Anti Clockwise
    Motor1Speed = map(JoyStick1YVal,512,1023,0,250);
    analogWrite (Speed1Pin,Motor1Speed);
  }

This is the output:

image

I'm getting the same symptoms.

Graeme

how are you powering all this? If you are trying to use the Arduino to drive the motors, you cant.

Yeah, so I've got a 9V supply feeding one side of the Receiver breadboard that supplies the L293D pin 8 and pin 16 for the motor supplies. That links across to the Transmitter Arduino Vin. The 5V from both Arduinos supply the Transmitter and Receiver respectively.

Only the Receiver Arduino has a USB plugged in.

Verily ground is shared for all devices.

1 Like

Make sure your 9V supply ground is connected to the L293D board AND the Arduino GND pin.

1 Like

Ok, so you got me thinking about the supplies. I've put another 9V power supply to the Transmitter Arduino Vin and the 433Mhz Transmitter takes its supply from that Arduino 5V.

The Transmitter breadboard and the Receiver Breadboard are now completely separated except for the wireless signal.

The Receiver breadboard 9V rail supplies the Arduino Vin and the L293D. The 5V rail supplies the 433Mhz Receiver. The two ground rails are linked.

The code is all un-rem'd and appears to behave but only motor no.1 spins. All four joystick values change when the joysticks are moved and display correctly on the Serial monitor.

I'll re-check all the jumper links and post up a schematic.

Graeme

Here's the Transmitter:

Yeah, I know it says Temperature Sensor, ignore that, it's the 433Mhz Transmitter.

Here's the Receiver:

Ditto Receiver/Temperature Sensor etc!

You have 9V connected to L293 pin 16, that should be 5V logic voltage. What is the source of 9V power?

When will people stop using smoke alarm batteries for servos and high current demand loads.

When they stop putting them in "starter kits" and beginners learn there's more to electricity than "volts", could be quite a while.

1 Like

That's useful, cheers.

The 9V supply is from a separate 9V PSU. It will be from a 9V battery on the real thing when it's on a boat. The L293 tutorial I worked through didn't differentiate between Vcc1 and Vcc2 with respect to voltages. It was my understanding that the voltage inputs were per motor.

I'll change VCC1 to a 5V supply.

Thanks

Graeme

What would you suggest I use to power 2 x 9V DC motors on a radio controlled boat in the middle of a lake?

Hopefully not the little rectangular (hearing aid) battery (PP3) , like this:

9V battery

I'm sensing bad feeling for the trusty little PP3 in this place! What's the problem with it?

Following your previous helpful post, I was this morning going to change the Receiver circuit to put the 9V supply on the Arduino Vin and on the L293 Vcc2. Then feed the L293 Vcc1 from the Arduino 5V pin.

But I need more coffee first!

Graeme

One reason, several more besides this
Continuing the discussion from Arduino Uno 9v batteries and sensors, an observation:

9V batteries aren’t ideal for devices that draw quite a lot of power…….
(They are made for smoke detectors which are low power……….)

Continuing the discussion from Arduino Uno 9v batteries and sensors, an observation:

Looks like we're going to need a bigger boat! :shark: :grinning:

I would suggest a rechargeable 3s LiPo pack at least 5000 mAh capacity.
But... I'm speaking from ignorance, we don't know anything about your motors' power requirements. :roll_eyes:

Excellent.

Thanks.

Graeme