About Me

My photo
They never fall who never climb.

017 MOTOR WITH PHOTORESISTOR CONTROL


017 MOTOR WITH PHOTORESISTOR CONTROL


March 21, 2011    (See also Companion Blog )

[Go BACK to Table of Contents] [ Next Experiment ] [ Last Experiment ]
           To have a photoresistor control a transistor, which in turn controls motor speed. Instead of hard code in the microprocessor controlling the motor, the idea is to read the value of a photoresistor. The photoresistor will change in resistance according to how much light is present. The brighter the light, the faster the motor speed, and if successful I will reverse the relationship to make the motor go slower.

           This is the first circuit I’ve tried that may have a practical value I can use. I would like a fan that runs faster as the day gets brighter and presumably hotter, a good idea in Florida. I know there is probably a less complicated schematic to depict my arrangement, but this is the version I understand. I’ve learned through study the motor diode, a 1N4001, is called a flyback control. Components which have a coil, such as a motor, will try to discharge that coil as the magnetic field collapses when power is removed, causing a spike of reverse current. The diode blocks that flow. Here is the breadboard schematic:



Theory:
           The photoresistor draws DC power from the 5V source, but limits this current by the amount of resistance caused by the prevailing light. Pin A0 sees this value which can range from 0 to 1023. It converts this to a transistor value 0 to 255 and sends this PWM signal to the transistor. This PWM value is output by pin D02 (digital pin 2) to the transistor base pin. The transistor reacts by controlling the current through the motor, hence the motor speed.

Practice:
           I’m getting some partially successful results. It took a bit for me to clue in that to make a digital pin use PWM, you have to write to it with the analogWrite command. Nobody took the time to make that clear, but I know it now. The photoresistor acts like an independent part of the circuit, don’t get thrown by how it uses common power and ground connections.
           The PWM signal was not getting through the standard 1000Ω resistor, so I took it down to 330Ω. The motor reacts instantly, but with strange behavior. Where I was expecting a smooth acceleration, the motor slows down when the light is blocked, but when the light returns, the motor does not run smoothly at all. It kicks and sputters. The good news is it does so in direct response to the photoresistor.


Conclusion:
           I determine the erratic motor response to be some wiring or component error that does not affect the validity of this experiment. The circuit includes a NPN transistor as a control and I’m not that familiar with transistors yet. Even without refining the motor operation, this experiment is a success, and the most complicated analog circuitry I’ve ever built myself.
Here is the code:

// Experiment 017 - Photosensor Transistor Controlled Motor
// Today is March 17, 2011
// This sketch corresponds to Minutes 017 blog

/* Adapted from several sources detailing the motor
* and sensor parts of this circuit, brough together here.
* Code format is my original work. The plan is to read
* a photosensor, send this data back to the Microprocessor
* and have it control the speed of a small motor. As
* the light gets brighter, the motor speeds up.
*/

////////////////////////////////////////////////
//Begin Initialization & Variables

int PhotoResistor = 0; //The photocell pin (Input)
int vPhotoResistorValue; //Storage for photocell value
int MotorPin = 3; //The Motor control pin (Output)
int vPWM = 0; //Convert 10 bit to 8 bit

//End Initialization & Variables
/////////////////////////////////////////////////

/////////////////////////////////////////////////
// Begin Main Program
/////////////////////////////////////////////////

void setup()
{
analogReference(DEFAULT);
pinMode(MotorPin, OUTPUT); //Arduino controls pin 2
Serial.begin(9600); //Enable serial monitor read
}
//-----------------------------------------------------------------------
void loop()
{
vPhotoResistorValue = analogRead(PhotoResistor); //Read the photocell
Serial.println(vPhotoResistorValue); //Display the value on serial monitor
vPWM = map(vPhotoResistorValue, 530, 680, 0, 255);
analogWrite(MotorPin, vPWM); //Ouput PWM value to motor control
delay(500); //Slow things down
}
//-----------------------------------------------------------------------

////////////////////////////////////////////////////
// END MAIN PROGRAM
////////////////////////////////////////////////////