I am trying to build a really simple circuit in which a dc motor is controlled by a spdt slide switch. The programme seems fine and I can get it to run - trouble is, I can't get the switch to control the motor switching on and off. Any clues? I'm probably doing something stupid! Thanks.
Thankyou. I know my circuit was non-existent but it seemed to be ok without the transistor - not that I would have built it that way! Anyway, it turned out that I had an error with my code. If I changed if(digitalRead(switchPin,HIGH) to ifdigitalRead(switchPin ==HIGH) the circuit behaved properly. I assume that the first statement set the switchPin to HIGH all of the time - which is why switching on/off made no difference and the second conditional statement tests the condition of the pin to see if it is HIGH or not.
Debs
PS I have now constructed it with the transistor switching circuit too. thanks again for your help.
debkeys:
Thankyou. I know my circuit was non-existent but it seemed to be ok without the transistor - not that I would have built it that way! Anyway, it turned out that I had an error with my code. If I changed if(digitalRead(switchPin,HIGH) to ifdigitalRead(switchPin ==HIGH) the circuit behaved properly. I assume that the first statement set the switchPin to HIGH all of the time - which is why switching on/off made no difference and the second conditional statement tests the condition of the pin to see if it is HIGH or not.
Debs
PS I have now constructed it with the transistor switching circuit too. thanks again for your help.
But still, you should not power the motor from the +5 Volts from the Arduino.
Motors should be powered by a separate power supply and a common ground.
debkeys:
Thankyou. I know my circuit was non-existent but it seemed to be ok without the transistor - not that I would have built it that way! Anyway, it turned out that I had an error with my code. If I changed if(digitalRead(switchPin,HIGH) to ifdigitalRead(switchPin ==HIGH) the circuit behaved properly. I assume that the first statement set the switchPin to HIGH all of the time - which is why switching on/off made no difference and the second conditional statement tests the condition of the pin to see if it is HIGH or not.
Debs
PS I have now constructed it with the transistor switching circuit too. thanks again for your help.
No, the code you need is
if (digitalRead (switchPin))
digitalRead takes a single argument the pin number, and returns a single truth value (HIGH and LOW
are synonyms for true/false and 1/0)
Don't connect a motor to an Arduino pin, the pin can't handle the current nor the inductive
voltage spikes, and will fail.