This example was written to control a motor from a touch sensor. More generically, it turns on a digital output based on the state of a digital input.
/*
Digital Input controlling DC Motor
by Tom Igoe
This example controls a DC motor on pin 13. If a digital input on pin 3
is HIGH, the motor turns on. Otherwise it turns off. In the original
example, the digital input was connected to a QT113 capacitive touch sensor.
Created 28 Jan. 2006
*/
int motorPin = 13; // this is the digital output pin for the motor
int touchSensorPin = 3; // this is the digital input pin for the touch sensor
int touchSensor = 0; // this is a variable to hold the state of the touch sensor
void setup() {
pinMode(motorPin, OUTPUT); // set the motor pin as a digital output
pinMode(touchSensorPin, INPUT); // set the touch sensor pin as a digital input
blink(3); // blink the LED pin 3 times to know that Arduino is alive
}
void loop() {
touchSensor = digitalRead(touchSensorPin); // read the touch sensor
if (touchSensor == HIGH) { // if the touch sensor is sending 5 volts
digitalWrite(motorPin, HIGH); // turn on the motor
}
else {
digitalWrite(motorPin, LOW); // turn off the motor
}
}