• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.


Annie Scalmanini Lab 2

Page history last edited by Annie Scalmanini 9 years, 3 months ago

Question 1:

a. What lines of code do you need to change to change the rate of blinking?



(the 1000 represents the amount of time that the LED is on and off, in milliseconds I think)


Se we would change the value in ( ) to change the rate


b. What circuit element do you need to add to protect the board and LED?
We definitely need a resistor in series with the LED to limit the amount of current across the LED

Question 2:

a. Which lines do you need to modify to correspond with your button and LED pins?
const int ledPin =  9;      // the number of the LED pin

const int buttonPin = 2;     // the number of the pushbutton pin

b. Modify the code or the circuit so that the LED lights only while the button is depressed. Include your code in your lab write-up.


Turns on and off a light emitting diode(LED) connected to digital  
pin 9, when pressing a pushbutton attached to pin 2.

The circuit:
* LED attached from pin 9 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  9;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);     

void loop(){
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {     
   // turn LED on:    
   digitalWrite(ledPin, HIGH);  
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW);

Question 3:

a) Which lines do you need to modify to correspond with your button and LED pins?

int ledPin = 9;    // LED connected to digital pin 9

Also, there is no button pin (or function) written into this code, but we could write it in ourselves, similar to the 'Button' code above - i.e. the LED fades when

b) How would you change the rate of fading?


for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 

We would change the value in ( ) above, and we could also change the '+=5' increment to change the rate of fading.

c - extra) Since the human eye doesn't see increases in brightness linearly how could you change the code to make the light appear to fade linearly?

Part B: Frankenlight

1. Super Bright LEDs

a. What is the minimum resistor size that should be used with these LEDs (hint: think about your voltage supply and what the diode voltage drop means)?


V = 5V - 3.4V = 1.6V

5V → resistor → forward drop (3.4V)

voltage across resistor = 1.6V

V = IR
1.6V = 0.02A * R
R = 80Ω

Want to draw 100mA for the motor, at 5V
so 5V = .1A(R)
R = 50Ω … need a 50Ω resistor on the end of the motor (where the 100Ω resistor currently is)




Comments (1)

redswood@... said

at 3:37 pm on Apr 23, 2011

Did you take apart an electronic device?
It is better to upload a photo of the device and a schematic of the inside.
And also implant an LED into the device, and upload a demo video.
For the bonus question:
This Lab used an 8-bit (from 0 to 255) Pulse-Width Modulation (PWM) to change the duty cycle of an LED, via one of the digital pins (of an Arduino) that support PWM output.
The example code that comes with the Arduino to fade an LED is as follows:
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
This code linearly changes the percentage of the duty cycle. However, humans perceive brightness not linearly (but approximately logarithmically) to the percentage of the duty cycle.
To have the LED have a linear fading effect, we can either use a lookup table that has been tested to give a linear perception, or use an exponential function to approximate that lookup table

You don't have permission to comment on this page.