Motor rotates only one direction with L293D controller - arduino

Hello I have an RC car which has two 3v motors (one for left/right and the other one for forward/back). The left and right motor is working fine but when I try to rotate the other motor it rotates only back. I've tried the motor separately and it works in both direction without the controller.
My code is the following:
int enablePinMotorAF = 3;
int in1PinMotorAF = 5;
int in2PinMotorAF = 6;
int enablePinMotorLR = 11;
int in1PinMotorLR = 10;
int in2PinMotorLR = 9;
boolean reverse = true;
void setup() {
pinMode(enablePinMotorAF, OUTPUT);
pinMode(in1PinMotorAF, OUTPUT);
pinMode(in2PinMotorAF, OUTPUT);
pinMode(enablePinMotorLR, OUTPUT);
pinMode(in1PinMotorLR, OUTPUT);
pinMode(in2PinMotorLR, OUTPUT);
}
void loop() {
//go forward ->not working
analogWrite(enablePinMotorAF, 230); //max speed
digitalWrite(in1PinMotorAF, reverse);
digitalWrite(in2PinMotorAF, !reverse);
delay(3000);
//go back -> working
analogWrite(enablePinMotorAF, 230); //max speed
digitalWrite(in1PinMotorAF, !reverse);
digitalWrite(in2PinMotorAF, reverse);
delay(3000);
//go right -> working
analogWrite(enablePinMotorLR, 230); //max speed
digitalWrite(in1PinMotorLR, !reverse);
digitalWrite(in2PinMotorLR, reverse);
delay(3000);
//go left -> working
analogWrite(enablePinMotorLR, 230); //max speed
digitalWrite(in1PinMotorLR, reverse);
digitalWrite(in2PinMotorLR, !reverse);
delay(3000);
}
Here is the wiring too:
Wiring
The green and orange wires are for a Bluetooth module.
Do you have any idea how can I solve this problem and to make it work?
Thank you.

To reverse the motors, you need four pins, two for each motor. On a readily-available L293 module, they are often labeled IN1, IN2, IN3, and IN4.
To make one motor go forward, you might set IN1 to 5V and IN2 to 0V. To reverse it, simply switch the inputs, IN1 to 0V and IN2 to 5V. In this case 5V is a digitalWrite(pin, HIGH).
Similar for the other two pins for the other motor. I start my answer with this because the wiring of what output pins to what input pins is vitally important.
The Enable pins is where you've gone wrong, it seems. Enable2 and Enable1 should be connected to the pins to which you're doing the analogWrite() but enablePinMotorAF = 3 for example connects to a motor signal input, not to Enable2 as it probably should. Start with fixing that... your two pins 3 and 11 should be connected to Enable1 and Enable2. You only need PWM on the Enable pins. The others should simply be activated with digitalWrite().
Once you get the Enable n pins connected to PWM, then you'll have a good PWM enable signal. Simply connect the other pins on the same side of the chip (IN1 and IN2 for Enable1 and IN3 and IN4 for Enable2), and turn them on and off with `digitalWrite(pin, HIGH) and you'll be good to go.

Related

Arduino - Light is staying on for more than expected duration

Code is suppose to Light the inbuit-LED at Pin 13 whenever Pin 5 is High, however i have encountered couple of problem.
While measuring Voltage though Digital Meter - one pin at arduino GND and other at 1,2,3,4. They are showing some non-zero values. Earlier triggering Pin was 4 and light was staying on all the time.
When Pin 5 is high (by connecting 5V Pin from Arduino to Pin 5) it lights the LED as it should, but if Pin 5 stays high for more than 1/2 second, light stays high for more than 0.5 second even after the Pin 5 is disconnected from 5V Pin.
int buttonState = LOW;
int light = 13;
void setup() {
// put your setup code here, to run once:
pinMode(gateopen,INPUT);
pinMode(light, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
buttonState = digitalRead(gateopen);
if (buttonState == HIGH)
{
digitalWrite(light, HIGH);
}
else
{
digitalWrite(light, LOW);
}
//delayMicroseconds(500);
}
As suggested earlier, use a pull down resistor. Or change your circuit to active low and use INPUT_PULLUP.
I believe you're using delayMicroSeconds, which means the led is just blinking waaay too fast for your eyes. If you want 'half a second' as you hinted on your query, you would be using delay(500) instead.
You are experiencing electrical noise, just like Juraj said, just put a 220 Ohm pull down resistor

How to turn motor counter-clockwise

Currently, my code below turns on the motor, delays for a bit then starts again. This is all being done in the clockwise direction, however how can I write my code so it can turn counter-clockwise?
int motorPin = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
startStopMotor(135);
delay(1000);
startStopMotor(0);
delay(1000);
}
void startStopMotor(int speed){
analogWrite(motorPin, speed);
}
By the code you share i'am guessing you are running a low power 5v dc motor... but you should edit your answer to give us what type of hardware you are using. This is not an answer but an idea of what you should be looking for... Basically on the motor i suppose you have, you have pin 1 and pin 2. Pin 1 is connected to a PWM signal and pin 2 is connected to ground. This configuration allows you to run your motor clock'wise. To run your motor counter clock'wise you need to invert the direction of the current basically have pin 1 connected to ground and pin 2 connected to a PWM signal.
Now there are multiple ways of doing this, i am unsure of the exact code to do this on an arduino but your pin 1 and 2 will be connected each to a PWM pin. In the code you will need to tell the arduino to put Pin 1 or 2 as a pullDown pin which basically mimics a ground thus telling the direction the other pin will output a PWM
this is not example code but it will give you an idea of what it should look like
void loop(){
//move clock'wise
pin1.pullup();
pin2.pulldown();
analogWrite(pin1, 180);
//move counterclock'wise
pin2.pullup();
pin1.pulldown();
analogWrite(pin2, 180);
}

Arduino keeps crashing

I've got an Arduino with a WS2812 hooked up to it, powered by the USB on my computer and I am trying to run the following code:
#include <FastLED.h>
#define NUM_LEDS 144
#define DATA_PIN 6
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.show();
}
void loop() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
fill_solid(leds, NUM_LEDS, CRGB::Red);
leds[dot] = CRGB::Black;
leds[dot] = CRGB::Blue;
FastLED.show();
leds[dot] = CRGB::Red;
delay(30);
}
}
void setAll() {
FastLED.show();
}
What this does is sets all the LEDs to red, then goes through each one turning it off, then to blue and then back to red.
For some reason, it gets X number of LEDs along and then crashes. By crashes I mean the Arduino disconnects itself from the computer, but the Arduino stays on with the LED strip still powered up.
Any ideas? This is a genuine Uno.
Also. If I plug the LED into the 3.3v pin, the animations works just fine, but the LED flashes black and doesn't complete the Blue part.
I never used the NeoPixels, so I'm not really sure about this, but I'm pretty confident these will solve your problem.
First of all, your program. I don't think it is doing what you think it should do. Try with this loop, instead:
void loop()
{
fill_solid(leds, NUM_LEDS, CRGB::Red);
FastLED.show();
delay(100);
for(int dot = 0; dot < NUM_LEDS; dot++)
{
leds[dot] = CRGB::Black;
FastLED.show();
delay(100);
leds[dot] = CRGB::Blue;
FastLED.show();
delay(100);
leds[dot] = CRGB::Red;
FastLED.show();
delay(100);
}
}
and remove the SetAll function, since it is useless.
Try this code with NUM_LEDS set to 5, and it should work.
Now the main problem: are you really using 144 leds powered by the USB? I suggest you to read this link about powering the neopixels. Particularly the part stating that at full brightness each neopixel draws 60mA. Doing the math, 144 neopixels draw at most 8.64A, so you need a 5V 10A power supply to power them all! a USB with 5V 0.5A will just shut itself down when you try to turn them on, thus giving you strange behaviors.
So lower the number of leds you are using (7 at most), or use an external power supply. And by external I do not mean use the barrel jack on the arduino, but connect a 5V 10A (or more amps) to the neopixel strip, then the ground and data wire to the arduino (not the +5v) and power the arduino through the usb port: it should work.
UPDATE:
According to the chat with the author, the problem was indeed the power supply
The datasheet from adafruit https://cdn-shop.adafruit.com/datasheets/WS2812.pdf says that you need a power supply between 6v and 7v but USB cannot provide more than 5v, I am guessing the arduino crashes because it cannot find enough power.
Can you try using an external power supply?

Sodaq - How do you toggle the "switched" groove connectors with a Sodaq Moja

How do you turn the power on and off in software for the "Switched" side of the grove connectors on a Sodaq Moja?
I know its a matter of "turning off the ground". What is an example of code that does that?
I figured out how to do it. While it is really not documented there is an accidental hint under section "2. Programming the SODAQ board" at http://www.sodaq.net/#!getting-started/c21ma.
"Pin 6 is normally used for switching power to the Switched row of Grove connectors, but for now we just use it because it has an LED too."
The digital pin 6 powers both the led and the actual grove connectors as well. Here is some sample code.
void setup()
{
pinMode(6, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(6, HIGH); // Turns on both the LED and the power
delay(400);
int value = analogRead(A0); // read your analog sensor that needs power
digitalWrite(6, LOW); // turns off the power
Serial.println(value);
delay(1000);
}

Arduino pull-up resistor not working

I am using an Arduino board to read out the value of a soft potmeter. (a strip that detects touch). This works perfectly fine as long as the strip is being touched (a resistance is added to the current).
When the strip is not touched, a completely random floating number is read by the analog pin. Forums mention that you have to add a pullup/pulldown resistor to cancel this effect, but this does not seem te be working. What is wrong with this code?
int potPin = 2;
int curval = 0;
// detect potmeter value
void setup() {
//enable pullup resistor, but still results in erratic output
//when potmeter is not touched
digitalWrite(potPin, HIGH);
//write to serial
Serial.begin(9600);
}
void loop() {
curval = analogRead(potPin);
// this works when the potmeter is being pressed (displays 0 to 1024)
Serial.println(curval);
delay(150);
}
Change
int potPin = 2;
to
int potPin = A2;
Your original use of "2" in both places is assigning Digital Pin 2 to pull-up and reading from Analog Channel 2. As "2" maps correspondingly to is PortD bit 2 and Analog Channel 2 (aka ADC2) is PortC bit 2. As shown below
digitalWrite(2, HIGH); // Pin D2
curval = analogRead(2); // AMUX Channel 2
where A2 shown below is interpreted as follows
digitalWrite(A2, HIGH); // Pin (A2 aka D16)
curval = analogRead(A2); // AMUX Channel 2 on Pin A2
On an UNO (ATmega328) the analogRead() function will interpret 0-7 as channels and will convert the pins A0 through A7 (D14-D21) to corresponding channels, to read from.
Note:
ADC6 and 7 are not available on the chip used on the UNO.
A0-A7 are alias for Digital 14 through 21. Where the labels A0-A7 are typically used.

Resources