L293 motor 1 and 2 stop working after using analog buzzer

Lost, I created a BT car, using HC05 and L293 motor shield on the UNO R3
I used the analog pins A0 to A5 for lights, SR04, HC05 and a passive buzzer.
All works well, but the moment the buzzer has sounded once, motor 1 and 2 stop working.
Everything else keeps working.
I have to press the reset buton on the motor shield, after that, all is back to normal, but the moment I activate the buzzer, the problem is back.
I tried to swap the buzzer with an other analog port, but the problem remains.
I used the passive buzzer, so I could play a little melody, instead of a single beep,

'''

#include <AFMotor.h>
#include "pitches.h"
#include <SoftwareSerial.h>
#include <Servo.h> 
#include "SR04.h"
#define TRIG_PIN A5
#define ECHO_PIN A4
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;//afstand tot sr04
int FrontLED=A3;
int i = 50;
SoftwareSerial HC05(A0,A1); // RX, TX
int horn=A2;
//initial motors pin
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

  int ledState = LOW;
  //unsigned long premillis = 0;  om de led te laten blinken
  //const long interval = 1000;

Servo myservo; 

char command;

void setup()
{
 
   myservo.attach(10);  
  myservo.write(115);
 pinMode(FrontLED, OUTPUT);
  //digitalWrite(FrontLED,LOW);
 //pinMode(horn, OUTPUT);
//digitalWrite(horn, LOW);
  HC05.begin(9600);  //Set the baud rate to your Bluetooth module.
  Serial.begin(9600);
}

void loop() {
  
  a=sr04.Distance();
  //Serial.println(a);
  if (a<=20){
    //digitalWrite(horn,HIGH);
    tone(horn,NOTE_C5, 1000);
    delay(1000);
    //digitalWrite(horn,LOW);
 //digitalWrite(FrontLED,HIGH);
 // delay(1000);
 // digitalWrite(FrontLED,LOW);
 // delay(1000);
 //unsigned long numillis = millis();

 //if (numillis - premillis >= interval)
// {
 // premillis = numillis;
 // if (ledState == LOW) {
 //   ledState = HIGH;

 // }
//  else
//  {ledState = LOW;}
  // digitalWrite (FrontLED, ledState);
 //}
 
  }
  if (HC05.available() > 0) {
    command = HC05.read();

    Stop(); //initialize with motors stoped
    digitalWrite(FrontLED,LOW);
    switch (command) {
      case 'F':
      
        forward();
        break;
      case 'B':
        back();
        break;
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
      case 'V':
        toeter();
        break;
      case '3':
        toeter();
        break;
      case 'K':
        links();
        break;
      case 'P':
        rechts();
        break;
      case 'W':
        lichtaan();
        Serial.println (command);
        break;
      case 'w':
        lichtuit();
        break;
      case 'N':
        scan();
        break;
       
    }
  }
}
int lookRight()
{
    myservo.write(0); 
    delay(500);
  //  int distance = readPing();
    delay(100);
    myservo.write(115); 
   // return distance;
}

int lookLeft()
{
    myservo.write(180); 
    delay(500);
  //  int distance = readPing();
    delay(100);
    myservo.write(115); 
   // return distance;
    delay(100);
}


void forward()
{
   myservo.write(115); 
   // return distance;
    delay(100);
  digitalWrite(FrontLED,HIGH);
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD);  //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD);  //rotate the motor clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void back()
{
   myservo.write(115); 
  //digitalWrite(FrontLED,HIGH);
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}

void left()
{
  digitalWrite(FrontLED, HIGH);
  myservo.write(170); 
  
  motor1.setSpeed(0); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(0); //Define lower velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(255); // Define lower velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void right()
{
  digitalWrite(FrontLED, HIGH);
  myservo.write(50); 
 
  motor1.setSpeed(255); //Define lower velocity
  motor1.run(FORWARD);  //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD);  //rotate the motor clockwise
  motor3.setSpeed(0); //Define lower velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(0); //Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
}

void Stop()
{
   myservo.write(115); 
  digitalWrite(FrontLED,LOW);
  motor1.setSpeed(0);  //Define minimum velocity
  motor1.run(RELEASE); //stop the motor when release the button
  motor2.setSpeed(0);  //Define minimum velocity
  motor2.run(RELEASE); //rotate the motor clockwise
  motor3.setSpeed(0);  //Define minimum velocity
  motor3.run(RELEASE); //stop the motor when release the button
  motor4.setSpeed(0);  //Define minimum velocity
  motor4.run(RELEASE); //stop the motor when release the button
}
void links()
{
  myservo.write(70);
}
void rechts()
{
  myservo.write(170);
}
void toeter()
{
  tone(horn,NOTE_C5, 500);
  delay(150);
  tone(horn,NOTE_F5, 500);
   delay(150);
   tone(horn,NOTE_A5, 500);
  delay(150);
  tone(horn,NOTE_C6, 500);
   delay(300);
   tone(horn,NOTE_A5, 500);
  delay(150);
  tone(horn,NOTE_C6, 500);
   delay(800);

  //digitalWrite(horn,HIGH);
   // delay(500);
  //  digitalWrite(horn,LOW);
}

void lichtaan()
{
//if (ledState == LOW) {
      ledState = HIGH;

//}
//else
//{
//      ledState = LOW;
//}
    digitalWrite (FrontLED, HIGH);
}
void lichtuit()
{
    digitalWrite (FrontLED, LOW);
}
void scan()
{
  digitalWrite (FrontLED, HIGH);
  for (int i = 50; i < 170; i++){
  
  myservo.write(i);
  delay(10);
  Serial.println(i);}
  for (int i = 170; i >49; i--){
    myservo.write(i);
  delay(10);
  Serial.println(i);}
  myservo.write(115);
  digitalWrite (FrontLED, LOW);
  }

'''

Is everything OK with the buzzer disconnected ?

I haven't studied your code, but the buzzer code will stop anything else from happening for 1.7 seconds. Can your code handle that?

That is not a problem, but after the buzzer everything still works, except M1 and M2 from the motorshield.

That does not make any difference, I get the same problem.

Hi, @gerard1960
Welcome to the forum.

Looks like a code problem, but can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Tom.... :smiley: :+1: :coffee: :australia:

Disconnection the buzzer really is not noticeable to the controller and thus cannot make a difference.

Your problem arises from the passive buzzer that has to be permanently driven high and low in code. Use an active buzzer or PWM or code like BlinkWithoutDelay for non-blocking operation of a passive buzzer.

This might not be just a software issue, it's posible that running the buzzer might cause a bit of a brown-out for the motor driver chip. I'd often recommend adding a bunch of decoupling capacitors, they help keep higher voltages in place without browning out when brief (milliseconds long) bursts of power get demanded in a circuit. But as the buzzer runs for a whole 1.7 seconds I expect there are further issues too.

Can you, for static testing purposes if not in the moving car, try providing separate power supplies for each of: A) the buzzer, B) the L293D and C) the arduino, a common ground between them but each powered by a separate voltage source. This would let you recognise if the demand for current created by the buzzer is causing problems.

Also, when you run the buzzer, do you always stop the motors first? Perhaps it may be only when you run both the motors and the buzzer that you get power supply trouble, if this indeed is power supply trouble.

I'd recommend getting something better than an L293, the L293 is a very inefficient way to drive a motor, huge internal voltage drop wastes a lot of power and means you can barely power a motor at all if your highest voltage supply is 3.3V or 5V. The TB6612FNG is a much nicer choice, you can buy breakout boards with the TB6612 already soldered on to them.


At first I used the active buzzer, without any problems.
Then I decided to build in a little melody, so I had to switch to the passive one.
From that moment the problem occurs.
I think the easiest wAy out is to switch back, but I am curious how it is possible that the buzzer creates the fault.
One note is enough to stop the 2 motors from running, after that only pushing the reset button on the motor shield helps to let the motors come back.
Then it is all fine, untill the buzzer goes off again.It makes no difference whether the car moves or not.

The sound generation (as is) blocks everything else. PWM works without blocking.

It's not the buzzer, it's the 'melody' - it's stuck till that's done.

Nope, it's stuck after the first beep and it does not come back on.
After just one beep the 2 motors stop from working and they don't come back untill I press the reset button manually.

The tone() function interferes with PWM at pins 3 and 11. Look here:

https://docs.arduino.cc/language-reference/en/functions/advanced-io/tone/

And this is independent from the pin that you use with tone(). Both PWM-pins are used by your ancient motor shield.
tone() and your shield cannot be used together.

I think this is the problem indeed. Pin 3 and 11 are used for motor 1 and 2.
Too bad for the tune, but I have to save that for an other project, without the motor shield.
Thanks a lot, now I can move on again. :grinning_face:

You do not need the tone() library to make sound on a passive buzzer.

Try a non-blocking timer to toggle the buzzer, something like...

  if (millis() - timer > timeout) {
    timer = millis();
    digitalWrite(buzzerPin, !digitalRead(buzzerPin);
  }

For now, I am going to swap to an active buzzer so I will have no interference on the pins needed for the motors.
For my next projects it is a good idea to use the non-blocking timer.
Thanks.

A millis() timer is not suitable to create a tone. Even with only 500Hz, the output must be toggled every ms. And if you use micros() instead, you need a really short loop() turnaround time to create a stable tone.

@MicroBahner - I respect your expertise, ...but... this "works" on a nano/uno (the sound is driving me nuts at this very moment) so a melody is only a matter of time to find the right values. Am I missing something? I am very good at missing things.

int index, buzPin = 7;
int tones[] = { 200, 300, 400, 500, 800 };
unsigned long timer, timeout = 100;

void setup() {
  randomSeed(analogRead(A0));
  pinMode(buzPin, OUTPUT);
}

void loop() {
  if (millis() - timer > timeout) {
    timer = millis();
    index = random(5);
  }
  digitalWrite(buzPin, HIGH);
  delayMicroseconds(tones[index]);
  digitalWrite(buzPin, LOW);
  delayMicroseconds(tones[index]);
}

But that's something very different from what you suggested in #16. And it's far away from 'non blocking'.