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

View

# Lab2_JorgeRamirez

last edited by 9 years ago

Stanford University

Jorge Alfredo Ramírez López

05774573

1. Blinking LEDs with the Teensy

Connect the Teensy to your computer using the USB cable. The Teensy comes preloaded with a version of the Blink program on it, so its LED should start blinking as soon as the USB cable starts powering the board. This LED is hardwired to pin 11 of the Teensy (note that this differs from the Arduino boards, which have the LED hardwired to pin 13).

The Blink program itself can be found in the Arduino IDE's example code folder under File->Examples->Basics->Blink. Check it out!

1. What line(s) of code do you need to change to make the LED blink (like, at all)?

If we want that the LED blink but with a frequency that seems like is always on then we have to modify the line’s 18 and 20 with half of the period of 80 Hz. So the time will be:

t=1f=180Hz=0.0125s

ton=0.01252=0.00625s

So the only thing that we have to do is:

1. What line(s) of code do you need to change to change the rate of blinking?

In order to do that we have to change the 18 and 20 lines of code, so as the function delay( ) is in terms of milliseconds we just have to write the number of milliseconds that we want the LED be in High and in Low state.

1. What circuit element would you want to add to protect the board and LED?

If we want to add some kind of LED or any device we must to add a resistor in order to limit the amount of current. If we don’t do that the Teensy board will be fail because maybe it doesn´t have possibility of give the total current that the LED or any device require as a maximum.

Now modify the circuit and program so that you can blink an external LED on pin 9. Don't forget about question (c) above!

The new program is showed below so the only changes that I made was erase the 11 pin form de PinMode and digitalWrite functions and write the 9 pin.

The resistor that I calculate was:

R=VI=5v-2.5v25mA=100Ω

2. Toggle LEDs on and off using the Teensy

With your LED still connected on digital pin 9, hook up a button circuit on digital pin 2, so that the pushbutton attaches from pin 2 to ground, and so that there is a 9K or 10K resistor attached between pin 2 and Vcc (Vcc is the supply voltage. In this case, it is provided by the USB. You can check out your Teensy pinout diagram in your kit if you're still confused). Use either the same circuit you used for the previous part for the LED or the alternative design below. The alternate circuit causes the "on" state of the LED to occur when Teensy pin = LOW, not HIGH, as before.

The Teensy pin configured as an input has a 'high input impedance.' This means that it can sense the voltage without affecting the circuit, like a probe.

Use the Button program (File->Examples->Digital->Button) to make your Teensy into a light switch.

1. Which lines do you need to modify to correspond with your button and LED pins?

I had to change the 29 and 30 lines which correspond to the button pin and t the LED pin. So the correct values are the following:

1. 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.

It´s pretty easy to do that, just change the state in the digitalWrite function to low when the button is in high state, and when the button has low state just change the digitalWrite to a High state.

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:

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED off:

digitalWrite(ledPin, LOW);

}

else {

// turn LED on:

digitalWrite(ledPin, HIGH);

}

}

3. Fading LEDs on and off using the Teensy

What about those "breathing" LEDs on Mac Powerbooks? The fading from bright to dim and back is done using pulse-width modulation (PWM). In essence, the LED is toggled on and off very rapidly, say 1,000 times a second, faster than your eye can follow. The percentage of time the LED is on (the duty) controls the perceived brightness. To control an LED using PWM, you'll have to connect it to one of the pins that support PWM output—which are 4, 5, 9, 10, 12, 14 or 15 on the Teensy.

1. Which lines of code do you need to modify to correspond with your button and LED pins?

What I did in the fading program was to put a conditional that reads if the button is in the High state then the LED will fade in from min to max values, and if the button is in Low state the LED will fade from maximum to minimum. The lines of code that I add are marked in yellow:

const int buttonPin = 2;

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

int buttonState = 0;

void setup() {

// nothing happens in setup

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop() {

if (buttonState == HIGH) {

// fade in from min to max in increments of 5 points:

// sets the value (range from 0 to 255):

// wait for 30 milliseconds to see the dimming effect

delay(30);

}

}

else {

// fade out from max to min in increments of 5 points:

// sets the value (range from 0 to 255):

// wait for 30 milliseconds to see the dimming effect

delay(30);

}

}

}

1. How would you change the rate of fading?

Just by changing the incremental rate of the for loop, so that means that the LED will be obtaining an analog voltage from cero to 5 volts in increases of 5 points. So every time that one cycle ends, the fadeValue will increase 5 until get to 255.

1. (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?

What I did was to change the increment rate from 5 to 1, so the effect of the fading looks pretty linear but it takes a little while to reach to the maximum value or minimum value.

I think you misunderstood the question. The idea is to address the eye's non-linearity to intensity. The eye response logarithmically to intensity, with higher response to small intensity at the beginning and less to high intensity. i.e., if we increase the step size linearly, the intensity would simply seem logarithmic.

Part B. Frankenlight

As the first part of this lab, you will need to hack apart an existing electronic device. Later this quarter, we'll setup times to go to Room 36 (one of Stanford's prototyping and modeling shops) for safety training and lab orientation. After that, you'll be able to hack apart most anything.

1. Super bright LEDs

We have included some nifty superbright LEDs in your kit. They are clear and have two leads. The link to their datasheet is here. They have a 3.2V forward drop, and a max current rating of 30mA.

1. 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.)

The minimum resistor that can be use is:

R=VResI=5V-3.230mA=60Ω

Try prototyping a circuit with the superbrights. (Be careful! They are bright enough that it hurts your eyes to look at them!)

For this part I used the same fading program to use the super bright LED, but I made some changes to the circuit so instead of using only the protection resistor I also used a potentiometer to decrement the amount of brightness of the LED.

const int buttonPin = 2;

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

int buttonState = 0;

void setup() {

// nothing happens in setup

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop() {

if (buttonState == HIGH) {

// fade in from min to max in increments of 5 points:

// sets the value (range from 0 to 255):

// wait for 30 milliseconds to see the dimming effect

delay(30);

}

}

else {

// fade out from max to min in increments of 5 points:

// sets the value (range from 0 to 255):

// wait for 30 milliseconds to see the dimming effect

delay(30);

}

}

}

2. Take apart your electronic device, and draw a schematic of what is inside.

a. Is there computation in your device? Where is it? What do you think is happening inside the "computer?"

The keyboard has an IC that must be who sends the signal to the computer, so maybe it sends two signals to the computer because the cable has four cables, one of voltage, one of ground and the others two could be the signal.

b.Are there sensors on your device? How do they work? How is the sensed information conveyed to other portions of the device?

Yes there is like some sheets that would be like a pressure sensors, so every time that we press a key the signal is sent to the IC that has the keyboard in order to process that information.

c. How is the device powered? Is there any transformation or regulation of the power? How is that done? What voltages are used throughout the system?

As I said the cable of the keyboard has two cables that are for the power, so the amount of voltage that needs is only 5v. The voltage is obtained of the computer so there is no need to convert the power.

d. Is information stored in your device? Where? How?

No, there is no information stored in the device, just some words that are on the PCB that indicate the place where resistors, LED’s or capacitors must be placed.

3. Using your schematic, figure out where a good point would be to hijack your device and implant an LED.

Due to the easy way to connect the keyboard to the Teensy 2.0 I decided to hijack the keyboard at the end of it using the cable that goes to the computer. So the schematic is:

We have perfboardsin the lab, which provide a handy way to connect your parts. (These are what we used during the soldering demo.) You may want to make your light using passive components (such as switches, resistors or potentiometers) rather than your microcontroller (also known as a μC), unless you think of a nice way to incorporate the μC into your design without soldering it inside of a light. (You'll need it back for future labs and projects!) If your design does require a μC, perhaps you can run a lead from your breadboard to the main light, although you'll lose portability that way. Clever use of components is encouraged!

Give yourself enough time to build your design. Consider re-visiting the lab during another session, so you can access components, tools, soldering irons.

I used a keyboard of the box, so what my device does is just to represent by a LED the number that I am pressing on the numerical keyboard. The form in which the LED represents the number is just bye blink the same number of times as the keyboard pressed.

The next image shows the serial terminal while my program is running, so the number pressed on the keyboard will be printed on the terminal:

Now here is the program:

#include <PS2Keyboard.h>

const int DataPin = 2;

const int IRQpin = 5;

const int ledPin = 9;

PS2Keyboard keyboard;

void setup() {

delay(1000);

pinMode(ledPin, OUTPUT);

keyboard.begin(DataPin, IRQpin);

Serial.begin(9600);

Serial.println("Keyboard Test:");

}

void loop() {

if (keyboard.available()) {

if (c == PS2_ENTER) {

Serial.println();

}else {

Serial.print(c);

char p= c-'0';

for(int k=0;k<p;k++) {

digitalWrite(ledPin, HIGH); // set the LED on

delay(500); // wait for a second

digitalWrite(ledPin, LOW); // set the LED off

delay(500); // wait for a second

}

}

}

}

Schematic:

Photos:

#### Benjamin Tee said

at 6:19 pm on Jul 24, 2011

Nicely done! I like your detailed report. Keep it up!