I'm currently working on a home project where I'm running an L293D with a set speed by PWM. I've run a few different versions already and seem to have had no problems setting the speed until now. I cant seem to find what I have done wrong.
The speed is currently set through the enable pin and the single motor is controlled by setting input 1 high and input2 low and vice versa for reverse. External power of 10 volts is attached to VS (supply) and and 5 volts from the arduino is attached to VSS (logic supply).
I've checked all the outputs and the correct voltages are coming from the arduino, heading toward the chip.
Negatives are only connected on one side.
I've also swapped chips to see if it might be an electronic fault. the same issue is still arises.
int reedSwitch1 = 7; // switch input changed from pin 1 - just to keep Serial happy
int reedSwitch2 = 2; // switch input
int motor1Pin1 = 5; // pin 2 on L293D
int motor1Pin2 = 6; // pin 7 on L293D
int enablePin = 3; // pin 1 on L293D
#include <Servo.h>
Servo pointservo;
// Variables will change:
int reedSwitchCounter = 0; // counter for the number of reedswitches
int reedState = 0; // current state of the reedswitche
int lastReedState = 0; // previous state of the reedswitche
int speed = 1;
boolean flag;
void setup() {
pinMode(reedSwitch1, INPUT);
pinMode(reedSwitch2, INPUT);
// set all the other pins using as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enablePin, OUTPUT);
pointservo.attach(11);
digitalWrite(motor1Pin1,LOW);
flag = true;//set flag when motor1Pin1 is LOW
digitalWrite(motor1Pin1, HIGH);
flag = false;//set flag when motor1Pin1 is HIGH
analogWrite(enablePin, speed);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
}
void moveForward() {
if (digitalRead(reedSwitch1) == HIGH && flag == true) { // once the reed switch is magnetised and the flag becomes true
//both L293 pins are switched to low. The locomotive waits
analogWrite(enablePin, speed); // then runs backwards
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
flag = false;
delay(2000);
analogWrite(enablePin, speed); // locomotive crawls by what speed is set
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
}
}
void moveBackward() {
if (digitalRead(reedSwitch2) == HIGH && flag == false) { // once the reed switch is magnetised and the flag becomes true
//both L293 pins are switched to low. The locomotive waits
analogWrite(enablePin, speed); // then runs backwards
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
flag = true;
delay(2000);
analogWrite(enablePin, speed); // locomotive crawls by what speed is set
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
}
}
void pointCounter() {
// read the reedswitch input pin:
reedState = digitalRead(reedSwitch1);
// compare the reedState to its previous state
if (reedState != lastReedState) {
// if the state has changed, increment the counter
if (reedState == HIGH) {
reedSwitchCounter++;
}
else {
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastReedState = reedState;
// turns the point every 1 detection of the reed by
// checking the modulo of the reed switch counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (reedSwitchCounter % 2) {
pointservo.write(82);
}
if (reedSwitchCounter % 2 == 0){
pointservo.write(94);
}
}
void loop() {
moveForward();
moveBackward();
pointCounter();
// program then repeats forward, backwards, forward etc....
}
Yea, That's the problem it still goes at full speed even when the speed is set to 1. As I said everything seems to be coming out of the arduino correctly. I think its how I've wired it. I'm gonna get another L293D this evening and try that. I fear I have blown something. I'll post a schematic this evening.
The boolean is to get the the motor to move backward and forwards when the motor is set to high or low && a reed switch is detected. Don't worry about that that section all works fine.
For some reason without this in the setup it just misbehaves. it does not detect the reed sensors and continues to run regardless of if they are seen or not. With code as above. Just the speed I'm struggling with. I have the code, just cant get the L293 to work as it did before.
videos to large to post unfortunately
Firstly I don't see any decoupling capacitors on the L293D supplies...
Secondly if the wires actually do run as drawn I'd expect interference from the motor on the
inputs from the reed switches - those signals go over the motor and don't run alongside their ground
wire.
Hi,
Check you protoboard, the red and blue lines should be continuous as in the diagram you have provided, BUT some have the lines broken halfway down the length, indicating that the connection is not continuous from end to end.
Please check this and can you post a picture of your project and can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Thank you for all of your help offered, but it seems by changing the pins to enable on 11 (pwm) and 12/13 for motor pins 1 & 2, and soldering the contacts directly to the chip. all is fine and i can control the speed again! I hold the breadboard responsible.