Multiple devices using 5v pin

So I got one problem here. I'm using the Arduino Uno and connected some things for a working self-driving car.

We got 2x servos (SG90, tower pro 9g Micro servo)
1x HC-SR04 supersonic sensor
1x HW307 mechanical relay
and at last 1x 5v DC engine

When connected (we're using the relay to shut the engine from it's power) it keeps spinning while distance is under the minimum distance (in cm). I tested the supersonic sensor, but it's working fine and producing right values (tested in the serial monitor).

Maybe someone of you can find a idea, how to fix that!

Thanks for looking into this :slight_smile:

#include <Servo.h>

Servo servolenkung, servosonar; //Servos
                                



int iRelais = 2;    // Relay
int iEcho = 3;      // Echo-Pin
int iTrigger = 4;   // Trigger-Pin




int iMinDis = 8;  //minimum distance in cm


int iDistance = 0;


int sonar() {
  int iTime;
  digitalWrite(iTrigger, LOW);
  delay(5);
  digitalWrite(iTrigger, HIGH);
  delay(10);
  digitalWrite(iTrigger, LOW);
  
  iTime = pulseIn(iEcho, HIGH);

  
  iDistance = (iTime/2) * 0.03432; //calculating distance in cm
  


  return iDistance;           

            
}



void setup() {
    servolenkung.attach(5);
    servosonar.attach(6);
  
    pinMode(iRelais, OUTPUT);
    pinMode(iTrigger, OUTPUT);
    pinMode(iEcho, INPUT); 
}

void loop() {

   //Setting this to high shuts the engine from it's power, meaning it does not drive now
  digitalWrite(iRelais, HIGH);
  sonar();


  
  if (iDistance < iMinDis) {
    digitalWrite(iRelais, HIGH);
    servosonar.write(165);
    servolenkung.write(1);
    

    sonar(); //calling the sonar method

    
    if (iDistance < iMinDis){
      servosonar.write(30); //servo under the supersonic sensor to the right side
      servolenkung.write(165); //spinning the wheels
      sonar();
      //distance-check

      if (iDistance < iMinDis){
        //lol here we are implementing a error-led
      }
    }
  }

  //setting the voltage of the relais to low, making the engine spin
  digitalWrite(iRelais, LOW);
  delay(1500);
  //servos back to 90
  servolenkung.write(90);
  servosonar.write(90);



  
}

Use separate power for at least the servos, relay and the motor. 4 x AA NiMH rechargeable batteries should work.

The 5V pin can only provide limited current. The servos need 650mA each on startup and who knows what the relay and motor take. Insufficient power is the cause of the majority of problems involving motors,relays and servos.

Steve

Yes definitely motors and such like should have separate power from sensors and logic. Its an ultrasonic
sensor - supersonic means travelling faster than sound!

You may have problems with interference from the motor - a 10nF ceramic cap across the terminals is
one way to reduce the effect of the sparking in the motor from upsetting the rest of the circuit.

Thanks to both of you :slight_smile: