photoresistor to control a dc motor

Hello all,

I am very new to arduino and im trying to figure some stuff out. I am enrolled in mark rober's creative engineering class and im trying to make the stock dc motor that comes in the basic arduino kit to turn on when it gets dark out using a stock photoresistor that also comes in the basic arduino kit. I know nothing about writing code but I am a quick learner. I followed the book on lesson 9 to make a dc motor turn on when using a switch. so I tried using that schematic and replacing the switch with the photo resistor and adjusting the code for what I can find scouring the bowels of the internet and to no avail.

circuit board = arduino uno
DC motor = stock from Arduino starter kit 6V; no load speed 12000 rpm
phototransistor = stock from arduino starter kit

and what I did not mention is I also want it to turn on an LED light when the motor turns on. I haven't added this complication yet to my current build because I was just trying to work through 1 problem at a time.

if it helps any, the idea of the project is when it gets dark outside it will trigger the motor to turn on which spins an arm and at the end of the arm is an LED light that stays on. the motor only needs to spin in 1 direction and a constant speed not variable. I will attach what I have so far but if it is easier to start from scratch I wouldn't mind either.

Thank you

TransistorMotor_bb.png

TransistorMotor_bb.png

trialdclgt.ino (300 Bytes)

1 Like

Hi,
I would make a voltage divider using the photoresistor and a 100k resistor and attach it to a Arduino analog pin as in the attachment. I would also have the MOSFET attached to a PWM pin so you can control the speed based on the analog value from the photoresistor. Here is more on photoresists with Arduino:

kgray9:
I would make a voltage resistor

I'm sure you meant to type voltage divider.

...R

Yes. sorry. I meant voltage divider.

Here is a very basic example code:

//mosfet pin
#define mosfetPin 9
//LDR pin
#define LDRPin A0

//tune these:
//LDR lowest value
int value1 = 900;
//LDR highest value
int value2 = 1023

void setup(){
//setting up the pins
pinMode(LDRPin, INPUT);
pinMode(mosfetPin, OUTPUT);
}

void loop(){
//turn the analog value into a 0 - 1023 range
int mosSpeedVal = map(LDRPin, value1, value2,0,1023);
//write this value to the MOSFET to control speed
analogWrite(mosfetPin, mosSpeedVal);
}

This might also help: Arduino Reference - Arduino Reference

ok Thank you!

I did everything you said, I cleaned up the code a little bit to get it to verify. when attached, the motor just runs and doesn't stop even if the sensor is directly under light. i swapped out the photoresistor a couple times to verify its not a bad sensor.

I do appreciate the fast response

//turn the analog value into a 0 - 1023 range
 int mosSpeedVal = map(LDRPin, value1, value2,0,1023);

@kgray9 Oops.

This may help.

Please remember to use code tags when posting code.

Watch the photo resistor output on serial monitor baud9600 and tune the two values in the code accordingly.

//mosfet pin
#define mosfetPin 9
//LDR pin
#define LDRPin     A0

//tune these:
//LDR lowest value
int value1 = 900;
//LDR highest value
int value2 = 1023;

void setup(){
Serial.begin(9600);
//setting up the pins
pinMode(LDRPin, INPUT);
pinMode(mosfetPin, OUTPUT);
}

void loop(){
//turn the analog value into a 0 - 255 range
int LDR = analogRead(LDRPin);
Serial.println(LDR);
int mosSpeedVal = map(LDR, value1, value2,0,255);
//write this value to the MOSFET to control speed
analogWrite(mosfetPin, mosSpeedVal);
}

That should definitely work. I forgot analogRead in the previous code

Please remember to use code tags when posting code.

That should definitely work.

You may also need to check the reference for analogWrite

Sorry about the code tags.
@nubsterperry. Did you want to control the brightness of your LED the same way as your motor? Did you just want it to turn off or on?

kgray9:
Sorry about the code tags.

You fixed that.
Now about that analogWrite.

Sorry. I forgot. AnalogWrite is from 0 - 255, not 0-1023. I fixed the above code.

just on and off. thanks.

im playing around with the values for the sensor and things seem to be reversed. as the light comes on the motor comes on and when the light goes off the motor goes off. i'm looking for the reverse and is there a guide to what each value in the map(LDR, value1, value2 ,0, 1023) mean. im assuming the last two are the range of the motor and the first two are on at value1 and off at value2??

thank you again

The map function is explained on Arduino reference here. Short explanation: map(inputValueToMap, inputValueLow, inputValueHigh, lowRangeMapped, highRangeMapped);

You can just reverse the two input values in the map function to reverse the fan to light reaction.
Example:
map(LDR, value2, value1, 0, 255);

Thank you so much for your help! It works. I am able to see the serial monitor than accurately adjust the set points to get what i want. you've been so much help.

now just one last feature is there a way we can add an LED light to illuminate as the same time as the motor turning on?

Your'e welcome! Glad it works.
When do you want the light to turn on? Like, do you want it to turn on when the motor first starts? Or when it is moving the fastest? Any specifics?

just right when the motor starts. nothing too fancy. thanks!

//mosfet pin
#define mosfetPin 9
//LDR pin
#define LDRPin    A0
//LED pin
#define LED       2

//tune these:
//LDR lowest value
int value1 = 900;
//LDR highest value
int value2 = 1023;
//LED turns on above this mapped LDR value (0 - 255)
int LEDOn = 20;

void setup(){
  Serial.begin(9600);
  //setting up the pins
  pinMode(LDRPin, INPUT);
  pinMode(mosfetPin, OUTPUT);
  pinMode(LED, OUTPUT);
}

void loop(){
  //turn the analog value into a 0 - 255 range
  int LDR = analogRead(LDRPin);
  Serial.println(LDR);
  int mosSpeedVal = map(LDR, value1, value2,0,255);

  //write this value to the MOSFET to control speed
  analogWrite(mosfetPin, mosSpeedVal);

  //LED
  if (mosSpeedVal > LEDOn){
    digitalWrite(LED, HIGH);
  }
  else {
    digitalWrite(LED, LOW);
  }
}

Make sure to tune the new value at the top for the LED also. When the mosSpeedVal value is above that preset number, the LED will turn on. Besides that, the LED will be off.

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