Running a Motor based on Voltage reading from sensor

Hi guys!

So, this is only the second time that I have used Arduino in my life so please forgive any cardinal sins.

Basically, I want to write a program that will read a voltage input from a sensor and then if the input is equal to zero, I need the motor to run. I do not get any errors when I upload the code to the Arduino, but the motor will not run.

here is my code.. please help me

int motorPin = 9;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
// If the voltage measurement is under zero, turn on motor

if (voltage = 0){
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, HIGH);
}
}

getting_there.ino (945 Bytes)

Well, first, I see this:
if (voltage = 0){
and it is not a compare. Perhaps you want if(voltage == 0){

Paul

You have several problems. First:

 if (voltage = 0){

This is SETTING voltage to zero, not testing if it IS zero. You want "==" rather than "=".

Second, It's unlikely you'll reliably get a reading of exactly 0. Much more likely you'll see some small value, perhaps less than 10 or 5. So you really want to check if it is below some reasonable threshold, perhaps 5 or 10, depending on what readings you actually see in use.

Regards,
Ray L.

What reading from the sensor is going to result in a voltage of 0? Why is it necessary to multiply and divide to get the voltage reading? The voltage corresponds to some reading from the sensor. Just use that reading in the if statement.

So you really want to check if it is below some reasonable threshold, perhaps 5 or 10, depending on what readings you actually see in use.

5 volts or 10 volts, when OPs threshold is 0, is hardly reasonable.

A reading from the pin of 5 or 10 might be.