Need help with photocell to run DC motor

Hey guys, need help with a little project to run a DC motor using a photocell with a simple operation of light=no motor, dark=motor.

But I'm having trouble finding some code to play with, any chance someone could point me in the right direction?

Found this that will read out the light level to level to LED, but how would I change this to run the motor?

int sensorPin = 0;    			// select the input pin for the photocell
int sensorValue = 0;  			// variable to store the value coming from the photocell
int LEDpin = 8;					//LED Pin is on the Digital i/o pin number 8

void setup() {
  Serial.begin(9600);      //Set baud rate to 9600 on the Arduino
  pinMode(LEDpin, OUTPUT); //set the LED pin as an output on digital i/o pin 8
}

void loop() {

  sensorValue = analogRead(sensorPin);  //get the value from input pin
  Serial.println(sensorValue);  //print the value to Serial monitor
  delay(2000);

      if (sensorValue < 300) //if there is darkness then turn led on

      {
        digitalwrite(LEDpin,HIGH);
      }
      else
      {
       digitalwrite(LEDpin, LOW);  //else, keep the led off
      }

}

In the simple case that you just want to turn the motor on or off, that code already does what you want. There just remains the question of how to wire the motor so it's switched by pin 8. A 5V relay switching the motor's (external) power is one way.

Using that code, can I just power the motor with a external psu and a transistor like so?

(and what transistor should I use?)

Yes that circuit and that code will work together.

As to the transistor you need on that can take the current of the motor. As you don't say what it is it is difficult to be definitive.
However a TIP120, while strictly speaking a darlington rather than a transistor, is good for a couple of amps.

Grumpy_Mike:
Yes that circuit and that code will work together.

As to the transistor you need on that can take the current of the motor. As you don't say what it is it is difficult to be definitive.
However a TIP120, while strictly speaking a darlington rather than a transistor, is good for a couple of amps.

Hey Mike, thanks for the info here and your site where i borrowed that motor layout :slight_smile:

was Just planning on using a very small motor DC motor upto 12v so can see me needing more then a couple of amps.

So....could I use any NPN High Voltage Gen Purpose like a MPSA42, MPSA92 or 2N3773?

was Just planning on using a very small motor DC motor upto 12v so can see me needing more then a couple of amps.

Do you mean "can't see me needing more then a couple of amps"
If you want more current then go for a FET.

Sorry, yes....

Should read, "I can't see me needing more then a couple of amps" (probably less then 1).

And.........

Think i already know the answer to this one, but would the arduino output pin have the current to drive a small v1.5 - 5Vdc motor with a load 450mA current?

Seeing you have a limit of 40mA that the arduino can safely supply, I will leave it to you to work out. :slight_smile:

XD thanks Mike!