Arduino R3 strange behaviour

Hy @ All

Just took my first adventure with Arduino. Attached L293d v4 Shield and uploaded a 4 Motor Test sketch found by google assuming it fits my needs:

//#include <Servo.h>
#include <AFMotor_R4.h>

// Common pin definitions for L293D shields
//int EN1 = 6; // PWM for Motor 1
//int IN1 = 7; // Direction for Motor 1
//int IN2 = 4; // Direction for Motor 2
//int EN2 = 5; // PWM for Motor 2
//int EN3

// Erstelle 4 DC Motor Objekte an den Anschlüssen M1, M2, M3, M4 
AF_DCMotor motor1(1);
AF_DCMotor motor2(2); 
AF_DCMotor motor3(3); 
AF_DCMotor motor4(4);


// Motoren auf Geschwindigkeit 0 setzen (aus)    
void setup() {

motor1.setSpeed(0);
motor1.run(RELEASE);  
motor2.setSpeed(0); 
motor2.run(RELEASE); 
motor3.setSpeed(0); 
motor3.run(RELEASE); 
motor4.setSpeed(0); 
motor4.run(RELEASE); 
delay(1000); 
} 

// Alle Motoren vorwärts 
// Geschwindigkeit 0-255 delay(2000); 
// Alle Motoren stoppen 

void loop() { 
motor1.stopMotors(0);
Motor2.stopMotors(0);
Motor3.stopMotors(0);
Motor4.StopMotors(0)
 
delay(1000); 

// Alle Motoren rückwärts 
moveBackward(200); 
delay(2000); 

stopMotors(); 
delay(1000); 
}

// Hilfsfunktion: Vorwärts 
void { 
moveForward(int speed) 
motor1.setSpeed(speed); 
motor1.run(FORWARD); 
motor2.setSpeed(speed); 
motor2.run(FORWARD); 
motor3.setSpeed(speed); 
motor3.run(FORWARD); 
motor4.setSpeed(speed); 
motor4.run(FORWARD); } 
// Hilfsfunktion: Rückwärts

void { 
moveBackward(int speed) 
motor1.setSpeed(speed); 
motor1.run(BACKWARD); 
motor2.setSpeed(speed); 
motor2.run(BACKWARD); 
motor3.setSpeed(speed); 
motor3.run(BACKWARD); 
motor4.setSpeed(speed); 
motor4.run(BACKWARD); 
}
 
// Hilfsfunktion: Stopp 

void { 
stopMotors() 
motor1.run(RELEASE); 
motor2.run(RELEASE); 
motor3.run(RELEASE); 
motor4.run(RELEASE); 
}



Starting from scratch there was no error in testing and uploading the sketch. The jumper for external power is removed. It´s a small drill accu with 14.4 Volts. As found in the specs for L293d shield current is supported up t16 Volts. The four DC Motors are 12 Volts for standard Blowers When the Arduino boots all Motors spin in different directions.

If I switch off the Accu-Pack the Motors spin at 5 Volts Arduino current.

I did a lot of searching for reasons the Test-Sketch doesn´t work. Servo.h is outdated and not longer maintained. AF-Motor_R4.h may be the wrong library. But most of other examples on the net are outdated and libraries i found missing the .h or the .cpp file expect in lower directories in the .zip files. Or they simply not fit my hardware. Maybe there is a resistor missing to force all motors to low at the beginning and the reset switches are without results.

I assume the code is not fitting the criteria simply to turn and stop the motors as expected. ;-(

For now i am rather clueless and begging for help to get the neccessary help to achieve a little success.

At least my english fits the criteria of this forum and the thread and post is not a reason for additional comments.

Thks in advance Ede

Hi @ede-beton.

Very cool! I like that test board you set up.

You don't use the Servo library in your sketch program, so this is completely irrelevant. Furthermore, you aren't using servo motors, so it is even more irrelevant.

In order to avoid any future confusion related to this, please remove the following comment from your sketch code:

The only function of a comments is to communicate information to humans reading the code, so removing this line won't affect the behavior of the program in any way.

This is problematic because the motors draw a lot of current, and in this case you must be powering them by power that is backfeeding through the 5 V regulator on the Arduino board.

There is a tutorial about the motor driver shield you are using here:

It contains this schematic of the power system:

https://lastminuteengineers.com/l293d-motor-driver-shield-arduino-tutorial/#:~:text=the%20Arduino%20like-,this%3A,-or%20the%20two

This would be a valid way to power the motors (assuming a sufficient power supply). However, in this diagram the power is being supplied to the system via the barrel jack on the board. From your picture, I can see that, with the Accu-Pack disconnected, you are instead supplying power via the USB socket of the board, resulting in this:

Beyond the problem of the USB supply on your computer being insufficient for the current draw of the motors, the bigger problem is that you are passing current through the 5 V linear voltage regulator on the board in the wrong direction (from the regulator's output pin VO to its input pin VI). This could damage the voltage regulator on the UNO R3. In the diagram above, the voltage regulator is marked "78L05Z", while your UNO R3 board actually has the
NCP1117ST50T3G, but the situation is the same regardless.

For this reason, I recommend removing the jumper on the board (which is marked "PWR Jumper" in the diagram above). To do that, just pull this yellow component up until it comes off the male header pins it is currently connected to:

This is make it so the motors can only be powered from the "EXT_PWR" terminal on the shield.

There are syntax errors here:

You have misplaced the opening brace ({). The syntax is correct on the previous function definitions:

Do you see the difference in where the { is placed?

Something that might be confusing is that there is a line break in the incorrect code (the Arduino IDE auto code formatter might have added this due to the incorrect syntax). If we apply formatting equivalent to the correct code, the incorrect code would look like this:

void { moveForward(int speed)

I'm guessing you are referring to the "AFMotor-Shield-R4-Compatible" library available from the Arduino IDE Library Manager:

I think it will be fine. Note this information from the library's readme:

This library provides full compatibility with the original AFMotor library API while supporting modern Arduino boards including the Arduino R4 WiFi. It maintains the same function calls and behavior as the original library, making it a true drop-in replacement.

This means you can use the high quality documentation provided by Adafruit as a reference

Note that the shield you are using is a clone of the Adafruit Motor Shield V1. So be careful to avoid following links that lead to information specific to the newer Adafruit Motor Shield V2 and its library, which are significantly different.

The code you have shared won't even compile. So this makes me suspect that you never managed to upload the program to your UNO R3 board. Thus whatever behavior you are observing from the motors is not caused by your sketch code.

Fix your code, then upload the program to the board. Make sure the upload finishes successfully. You will know this when you see the "Done uploading." notification in the Arduino IDE window:

Note that this notification is hidden after a few seconds, so you might miss it if you aren't watching. If you miss it, you can click the :bell: icon at the right side of the status bar to show the hidden notifications again.

The datasheet says the L293D handles motors 4.5V to 36V. Are the motors wired "in different directions?"

Seems to be a missing semicolon on the last of these lines here as well.

Hy @ Bitseeker, XFPD and Ptillisch :+1:

Thank you both for your comprehensive and knowledged answers. Most mistakes you named in your answers are hitting the bullseye. I will need a little to correct all the mess i did. When all above is cleared, i will be back to ask in detail about my misunderstandings. Have a smart start into the coming week. And thks again. A pitty here is no Thks-Button or Bell i would hit it twice...!

Best regards Ede