Homework 03 – LED/Servo Motor Controlled by Variable Resistor – Casey Henrichs

31 Oct


The servo motor powered by light sensor.

The greater the “val map” the further the rotation. Change 179 value to 1024 for greater rotation.

#include <Servo.h>

Servo myservo;

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup()
myservo.attach(9); // attaches the servo on pin 9 to the servo object

void loop()
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)

I used the light sensor to fade an LED in and out. It was interesting to play around with the “analogWrite” value divided by. The greater the number the less variation seen in brightness.

const int LED = 9;

int val = 0;

void setup () {
pinMode (LED, OUTPUT);

void loop () {
val = analogRead (0);
analogWrite (LED, val/2);
delay (10);


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s