How do I make a Dc motor controlled by photo resistors?

I just started this, and i really need help, i have already put in the code for the photo resistor to work when light is near to it that the LED will light up, i need to know is it possible if the LED is taken place by a DC motor to work and how am i gonna program it to work in the code? like when light appeared the DC motor will turn on, just exactly like the LED would lit up but instead its taken place over a DC motor, how am i gonna work it out and what are the components need to buy?? i really need help, thanks

Does the motor turn in only one direction or must it be able to run forward and reverse? What are the specs of the motor (rated voltage and stall current)? You will need a motor driver and an external power supply for the motor as an Arduino can't safely supply the current to drive a motor. It would help us to better understand what you want to do if you post the code that you have so far.

Please use code tags when posting code. See the "how to use this forum-please read".

You should be able to use more or less the same code. Switching on an LED and switching on a motor aren't very different.

But you will need to arrange suitable hardware to switch the motor and to recommend that we need to know more about the particular motor you're using. A link to the motor or its datasheet would help. There many different types and sizes of DCmotor.


wow i just read, "how to use the forum, please" what a great information this website is awesome man
anyways to get back of my problem

firstly here is my code:

 int lightPin = 0;
          int ledPin = 9;
void setup () {
pinMode(ledPin, OUTPUT);

void loop () {
int threshold = 400;
if(analogRead(lightPin) >
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, LOW);} }


i got this code online not by my own. oh crap i just remembered, this photoresistor works when Light is present the Led will light up, thanks so much guys for answering my question, really appreciated, (rip my english) i been figuring it out day and night with lil less sleep, doing my project and next week is my exam, this is very sad actually.. im the group leader and my team is struggling to do other parts(note for information for the lectures about the project) yet i haven't complete my part yet, it's really my fault at this moment, i should just go solo yet they needed an idea. (the group is 3 ppl) those who have no idea what to do they joined those who have idea. and they begged to join. now im doomed because of me delaying and focusing on my career rather than finding X i am finding Y :cry: :cry:
anyways this is just to give an advice for those doing their projects, PLEASE dont ever delay and doing it last minute, remember other people is with you, First COMPLETE X THAN Y. sigh, i believe there is still hope if i can get this code and the schematic thingy done, if yes than i would really appreciate it allot, saving my team along.

Ground Fungus

Details of R300C Mini DC Motor
DC1.5-6v (High Speed)
Spec: 3V around Rpm3500
6V around Rpm7000
DC1.5-6v (High Speed)
Spec: 3V around Rpm3500, 6V around Rpm7000

Steve and GroundFungus how to get the right motor driver? for my Arduino Uno, and here i found the similar one that i did but instead i kept 5v directly on the photo resistor is that the right thing? , so yea

You could answer the question "Does the motor turn in only one direction or must it be able to run forward and reverse?". That would tell you whether you needed a full scale motor driver or just a simple MOSFET to drive the motor.

Reading the photoresistor won't change. If it works now it will still work.

And as groundFungus already said you need some external power supply (4 x AA batteries often works) for the motor. You can't run it direct from the Arduino 5V pin.


The specifications for the motor leaves out the stall current. I looked on the net and could not find a stall current spec. We could guess at a stall current, but the engineer in me will not allow that. Stall current can be estimated by measuring the motor winding resistance and dividing the supply voltage by the measured winding resistance.

Once that we know the stall current, supply voltage and whether the motor needs to be reversible we can choose a motor driver.

As to your code:
You should use the notation A0 for the analog input. It does not matter to the compiler, the compiler knows what you want to do when you do analogRead(), but it makes it more cleat to the human that is reading your code.
Proper indenting the code makes it much easier to read. Use the autoformat option (CTRL-T or Tools, Auto Format) in the IDE to format the code in a consistent style.
The code, as shown, will not compile because of an extra curly bracket at the end. Before posting code, make sure that it will compile. If it won't compile, say so and post the error(s) in code tags.
You will save 4 precious bytes of SRAM by making pin assignments const byte instead of int. It doesn't much matter on your short sketch, but it is good practice that will serve, later, when writing larger programs.

const byte lightPin = A0;
const byte ledPin = 9;

void setup ()
   pinMode(ledPin, OUTPUT);

void loop ()
   int threshold = 400;
   if (analogRead(lightPin) > threshold)
      digitalWrite(ledPin, HIGH);
      digitalWrite(ledPin, LOW);

oh cools, thanks so much Groundfungus, so is that mean i can use the dc motor placed with the Led? or i need a MOSFET for that before i do that? like what Slipknot mention

That is your code with corrections to the items that I mentioned. The diagram looks OK. Does the code do what you want in terms of lighting the LED?

how to get the right motor driver?

You still have not answered the question of whether the motor turns hon ways or not. And we need to know the stall current and the motor supply voltage.

It is frustrating when one is trying to help, but the helpee does not seem to read what one posts.

You cannot drive a motor with a digital output like you can an LED. So, for a motor that will only turn in one direction replacing the LED circuit with a MOSFET circuit will work. Note that I say MOSFET circuit as there is more to it than just the MOSFET. See below:

dc motor driver.jpg

But we can’t choose a MOSFET until we have the required motor specs.

sorry i missed that, yes i need it both ways in reverse, so when light is present it works and when light is absent it turn the other way , motor supply current is at 6v
what is a stall current?

Then a simple MOSFET circuit will not work. The MOSFET is for on an off. For forward and reverse, an H-bridge is required (Google H bridge).

Stall current is the current drawn by the motor with the shaft locked. That is the maximum current that can be drawn by the motor. Stall current is also drawn at motor start up, momentarily, so any driver must be able to, safely, supply that amount of current.

so is this type of H- Bridge i need to get? , thanks for the reply :slight_smile:

so i need to get a H-bridge and a MOSFET for it to turn on and off? or just an H-bridge is enough?

I finally found some info on the stall current of that motor here. Stall current (locked current) is listed at 910mA so, yes, the L298 driver will "work". I put work in quotes because the L298 is ancient technology and is very inefficient. The L298 drops 2 to 4 volts so to get 6V to the motor, one must have an 8V (minimum) motor supply. The 2 to 4 volts that are dropped are dissipated as heat.

This modern MOSFET output driver would be a much better choice.

wow thanks man, i went to the city to search this type and couldnt find any motor driver in any eletronic shops. if i were to get your DRV8838 Single Brushed DC Motor Driver Carrier this stuff needs to be ordered and it can take for four days or maybe a week, which i can't , 13 AUgust is my exam, so i have decided to just have a 1 direction only, since i dont have much time , using a voltage regulator(MOSFET) am i able to drive this motor? with the photo resistor in there. thanks, i just bought 2 diode, 10v 22uf capacitor, 6v & 12v voltage regulator and 560 ohm resistor, 1k, 220ohm and power supply of 6v AA, can this be in use for 1 direction motor?

voltage regulator(MOSFET)

A voltage regulator is not a MOSFET and a voltage regulator is the wrong part for switching a motor on and off.
What are the part numbers of the regulators?

A PN2222 bipolar transistor should be commonly available if you can’t find a real MOSFET. The MOSFET should be a logic level device capable of 1A current.

Circuit for controlling a motor with PN2222.

npn motor driver.jpg

crap.. well i have a P2N2222A transistor its the same right? thanks for the reply, so this could be possible with the photo resistor? what is the code for the adurino