Runs fine after uploading sketch, but not after restart

Hello,

I've setup a DC and Servo motor that fires when motion detected from PIR sensor. Works fine after uploading the sketch. However, If I turn things off, it doesn't work when I turn things back on. I have to re-upload the sketch to get them working again.

Any suggestions?

Thanks,

Keith

Sounds like a power issue. When uploading you have 2A of USB power available. When you disconnect and restart you're working off different power.

A hand drawn schematic and some good clear pics of your wiring would be a real help.

Please, before you post ANY code, use < ctrl T > to format & indent your code in the IDE before pasting it here. Also be sure to use the </> (code formatting) button before you paste here.

TYVM!

1 Like

Thanks for your response. Apologies in advance for misusing terminology. Anyway, here are a few pictures as well as my code. Good luck making sense of this:

This scheme is using a diode on the DC motor and running through a transistor.


I'm using a 9v battery on the DC motor and powering the Arduino from the computer.
The PIR sensor is on Pin 9,
the Servo is on Pin 7 and
the DC motor on Pin 2

Here is the sketch:

const int pirPin = 9;
const int motorPin = 2;
int switchstate = 0;
#include <Servo.h>
Servo myservo;


void setup() {
  myservo.attach(7);
  pinMode(pirPin, INPUT);
  pinMode(motorPin, OUTPUT);
}

void loop() {
  switchstate = digitalRead(pirPin);
  if (switchstate == HIGH) {
    digitalWrite(motorPin, HIGH);
    //High means motion detected
    myservo.write(90);  // move servos to center position -> 90°
    delay(500);
    myservo.write(30);  // move servos to center position -> 60°
    delay(500);
    myservo.write(90);  // move servos to center position -> 90°
    delay(500);
    myservo.write(150);  // move servos to center position -> 120°
    delay(500);

  } else {
    digitalWrite(motorPin, LOW);
  }
}

Please let me know if I'm not following the correct protocol.

Thanks,

Keith

You are using a 9 volt type 6F22 battery, are you serious!? This battery is not suitable at all, especially to power motors.

2 Likes

You can't power a motor directly from Arduino. Also the 9V batteries don't have the current. Use a real power supply and things will improve. You got a schematic? (Hand drawn is ok) I don't understand the diode on the motor.

I appreciate the comments. I was following another's work via Youtube. If a 9V battery is a bad idea, what is a better way to go?

Keith

Thanks a lot for taking the time to comment. I was following someone's work via youtube. Here is a drawing. I don't know how to do schematics. Sounds like I need to learn how. A general consensus is saying that a 9V battery is unacceptable. What is a the preferred method?

Thanks again for your commets.

KF

Is this the video ? https://www.youtube.com/watch?v=kggohWCtYuQ
That is not with real hardware, but in a simulation with Tinkercad.

Let's see what Tinkercad thinks about a 9V battery:


That is 6 amps for a single 9V battery !

I can put many 9V batteries on top of each other and connect them all together:

Formal schematics are a PITA. Draw it out rough on paper, take a pic and post it. Just show all the parts.
Use a USB power supply or "wall wart" that can supply sufficient current indefinitely.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.