Making Things Interactive

February 21, 2008

assignment 5: solenoid & servo

Filed under: 5: Making Motion,Assignments,Gee Kim — gskim @ 6:11 pm


I simply got my motors to work separately with two different sensors, but with one program.

The solenoid has a digital input from a photo sensor. The solenoid turns on and off depending on if it is dark or light.

The servo has a analog input from a potentiometer. The servo turns in ratio to the potentiometer.

here’s the code:

int servoPin = 3;     // connect servo to pin 3
int sensorPin = 2;    // connect potentiometer to pin 2
int solePin = 12;     // connect solenoid to pin 12
int photoPin = 4;     // connect photosensor to pin4

void setup ()
  beginSerial (9600);            //
  pinMode (servoPin, OUTPUT);    // set servo as analog output
  pinMode (sensorPin, INPUT);    // set potentiometer as analog input
  pinMode (solePin, OUTPUT);     // set solenoid as digital output
  pinMode (photoPin, INPUT);     // set photosensor as digital input

void loop ()
   int val = analogRead(sensorPin) / 11;    // set "val" equal to the analog read of the sensor divided by 11
   Serial.println(val);                     // print the value of "val" on the serial monitor
   analogWrite(servoPin, val);              // as the value of the sensor changes, the servo should spin according to "val"

   if (digitalRead(photoPin == HIGH))       // if digital read of photosensor is bright
   { digitalWrite(solePin, HIGH);}          // then turn solenoid on
   else                                     // otherwise
   { digitalWrite(solePin, LOW);}           // turn solenoid off

here’s the circuit diagram:circuit1.jpg


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: