attiny light sculpture project - part i (setup)

4
Name: _______________________________ Date: ___________ Build your own ATTiny Light Sculpture! Part I Introduction Using a programmable microcontroller, let’s see how you can combine color, shape, form, and space to create your own works of art! Traditionally, many have created projects like this using a full-blown Arduino - a microcontroller capable of handling lots of different inputs or outputs. These are great, but still somewhat costly (~$20). So instead, we are going to use the ATTiny85, the little sister to the Arduino Uno. ATtiny85 vs. Arduino Uno The ATTiny85 is...well, tiny! It is a single programmable chip that consists of just 8 pins -- 5 of which are controllable. This means we can write programs / code that control the behavior of these pins to turn LEDs ON or OFF, read sensors, or move motors! What makes it real nice is that it’s a self contained controller. All we need to do is hook up power to it. The Arduino Uno (or the SparkFun RedBoard) uses the ATMega328 microcontroller. This chip has 32 pins -- 20 of which are controllable! The Arduino Uno also has a lot of extra supporting hardware on-board including a voltage regulator, oscillator crystal, a programmer, and a hand-full of other components. While these are nice to have, for most projects, the ATTiny is perfect! ATTiny85 Pins The ATTiny85 has 8 pins in total -- 4 on each side. The pins are designated as follows. Look at the top of the chip closely. Notice that there is a either a notch or a small dot near one side? This indicates the “up” direction on the chip. learn.sparkfun.com

Upload: brian-huang

Post on 24-Jan-2018

242 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: ATTiny Light Sculpture Project - Part I (Setup)

Name: _______________________________ Date: ___________

Build your own ATTiny Light Sculpture! Part I

Introduction Using a programmable microcontroller, let’s see how you can combine color, shape, form, and space to create your own works of art! Traditionally, many have created projects like this using a full-blown Arduino - a microcontroller capable of handling lots of different inputs or outputs. These are great, but still somewhat costly (~$20). So instead, we are going to use the ATTiny85, the little sister to the Arduino Uno.

ATtiny85 vs. Arduino Uno The ATTiny85 is...well, tiny! It is a single programmable chip that consists of just 8 pins -- 5 of which are controllable. This means we can write programs / code that control the behavior of these pins to turn LEDs ON or OFF, read sensors, or move motors! What makes it real nice is that it’s a self contained controller. All we need to do is hook up power to it. The Arduino Uno (or the SparkFun RedBoard) uses the ATMega328 microcontroller. This chip has 32 pins -- 20 of which are controllable! The Arduino Uno also has a lot of extra supporting hardware on-board including a voltage regulator, oscillator crystal, a programmer, and a hand-full of other components. While these are nice to have, for most projects, the ATTiny is perfect!

ATTiny85 Pins The ATTiny85 has 8 pins in total -- 4 on each side. The pins are designated as follows. Look at the top of the chip closely. Notice that there is a either a notch or a small dot near one side? This indicates the “up” direction on the chip.

learn.sparkfun.com

Page 2: ATTiny Light Sculpture Project - Part I (Setup)

For the chip to operate, we need to supply power to it. Connect the positive side of your power supply to VCC and the negative side to GND. The ATTiny can handle voltages between 1.8 - 5.5 VDC. The remaining pins -- labeled 0, 1, 2, 3, & 4 -- are programmable. Using the Arduino programming environment, we can write code that will control the voltage to these pins. The last pin, labeled Reset, is for exactly that. When this pin is connected to ground (GND), the chip resets. For now, we’re just going to leave this one alone.

Let’s get coding! Open up the Arduino IDE (Integrated Development Environment). The icon should looks similar to this symbol here.

Before we write any code, let’s first configure the programming environment. First, let’s set up the board type. Click on the Tools Menu → Board → ATtiny85 (internal 1 MHz clock). Next, configure the Programmer. Click on the Tools Menu → Programmer → USBtinyISP. Note: If you are using a Windows PC, you will need to install a driver for the programmer. For directions, visit our hook-up guide at: http://bit.ly/tinyAVRinstall

Typing in Your First Lines of Code Carefully, copy this code into your Arduino window. Be very careful with spelling, capitalization, and punctuation. All characters and punctuation marks are required for this program to run. Any of the “grey colored” text that is preceded by // can be ignored or left out.

learn.sparkfun.com 2

Page 3: ATTiny Light Sculpture Project - Part I (Setup)

Ok, now how do I connect that chip to my computer??? Ah… yes, we will need a programmer to do this. There are a few ways of programming the ATTiny, but the easiest way is to use the Tiny AVR Programming Stick. Remember that dot on the chip? Plug the ATTiny85 chip in so that the dot or notch lines up with the notch on the stick. You might need to adjust the legs of the chip so that it fits. Now, go ahead and plug it into your computer. In Arduino, click on the Upload button. Watch the status messages at the bottom of the window. It will indicate when it is Done uploading. If you copied the code incorrectly, you might see error messages. Double-check the code. Remember that spelling, capitalization, and punctuation must be exactly as it is in the example.

If the code uploads successfully, you should now see a blinking LED on the corner of the Tiny Programmer! OK -- now, let’s look at adapting this and making our own light patterns!

learn.sparkfun.com 3

Page 4: ATTiny Light Sculpture Project - Part I (Setup)

Play with Code Let’s take a more in depth look at the lines of code here. There are two functions that are required in every Arduino sketch. These are void setup() and void loop(). A function is a way to group several instructions together. The instructions are grouped together using two curly braces { }.

void setup() { pinMode(0, OUTPUT); }

This first section of the code configures the chip. Any code that is between the curly braces { } after void setup() will run exactly one time. The 5 controllable pins on the ATTiny are all generic -- so, we have to configure them as either an INPUT or OUTPUT.

void loop() { digitalWrite(0, HIGH); delay(500); digitalWrite(0, LOW); delay(500); }

The void loop() function repeats over and over. After the setup() is run, the four lines of code here repeat continuously. There are two commands / instructions here: digitalWrite(0, HIGH); sets the voltage on pin 0 to a

HIGH state VCC. digitalWrite(0, LOW); sets the voltage on pin 0 to a LOW state or 0 Volts.

delay(500); pauses the program for 500 milliseconds

before going to the next instruction. The ATTiny85 runs at 1 MHz (1 Million instructions per second -- or, 1 µs per instruction).

Challenge A 1) Flash! Modify the code example so that the LED blinks at a rate of 100 times per

second. What do you see? Is the LED still blinking? Adjust the blink rate until you can just barely see it blinking. What is time period for the blink?

2) Sweet Heart. Now, adjust the sequence and timing of the on (HIGH) and off (LOW)

cycle to replicate a heart-beat pattern. (Hint: a heartbeat has two distinct beats).

3) Morse Code Challenge. Morse code is a method for transmitting text using a sequence of short (dot) and long (dash) beeps, blips, or flashes. In the early 1830s, the electric telegraph system was invented. Morse code was used to transmit text across long distances. Encode a short word, your name, or a message. See if you can communicate a message across the room to another student. In emergencies, the code SOS is internationally recognized as a distress signal. Hint: Be sure to have a noticeable pause between letters and between words.

learn.sparkfun.com 4