SACC

/ / navigation

LED Basics
Button Basics
Shooting Star

LED Basics



© Royalty Free/CORBIS
© Royalty Free/Corbis

Microcontrollers

Microcontrollers can be thought of as very small, inexpensive computers which may be programmed to control systems such as cell phones, microwave ovens, toys, automotive systems, etc. by sensing input from the real world and controlling those devices based on that input. Most electronic devices you use today have a microcontroller in them of some form or another. A typical household has upwards of 25 to 50 microcontrollers performing embedded control in numerous appliances and devices.
Microcontrollers are easy to use with simple sensors and output devices, and they can communicate with desktop computers fairly simply as well. They're very useful for when you're designing a simple interactive device that doesn't need the full power of a desktop computer, but does need to be smaller or cheaper.

How it works

A program is written in the Arduino, a special program running on your computer that allows you to write programs for the Arduino board in a simple language modeled after the Processing language. When you press the button that uploads the program to the board: the code you have written is translated into C, and is passed on to the avr-gcc compiler, a piece of open-source software that translates the code into the language the microcontroller can understand. This last step is quite important because it's where Arduino is making your life simple and hiding away as much as possible of the complexities of programming microcontrollers.



The Interface:

Screenshot0003.png

If you need Help with you program...

Hilite a command and then click on Help menu and select Reference:



Help


Common functions

  • pinMode()—set a pin as input or output

  • digitalWrite()—set a digital pin high/low

  • digitalRead()—read a digital pin's state

  • analogRead()—read an analog pin

  • analogWrite()—write an "analog" value

  • delay()—wait an amount of time

  • millis()—get the current time


Arduino Programming Notebook



Basic Sketch Structure

  • Declare variables at top

  • Initialize values
    • setup()— run once at beginning, set pins

  • Running
    • loop()— run repeatedly, after setup()






Read the following:

There are 12 digital I/O (Input/Output) pins on the Arduino chip. These are the pins through which input and output devices may be connected. Depending on the code that is written, each pin may act as an input to read a device, or as an output to control a device. You will begin by using a very common and simple output device, the LED: Using an Active High LED, connect pin 8 to a 220 resistor, which connects to one side of an LED and connect the other side of the LED to ground:



An LED is a diode, meaning electrons can flow in only one direction. In other words, polarity is important. The LED should have a flat side indicating the cathode or negative terminal. Also, the anode (positive terminal) generally has a longer lead than the cathode.
led.jpg


Here is the code to light the LED:
/* blink an LED*/

int LED1=8;

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

void loop(){
    digitalWrite(LED1,HIGH);
    delay(500);
    digitalWrite(LED1,LOW);
    delay(500);
}
Code Explanation
/* */ A comment
digitalWrite(8,HIGH) defines the pin to be an output and sets it to a HIGH state, digital 1 or 5V
digitalWrite(8,Low) defines the pin to be an output and sets it to a LOW state, digital 0 or 0V
delay(500) instructs the chip to wait for the defined number of milliseconds (1/1000 seconds).

Modify the code so that the light is on for 50 milliseconds and off for 50 milliseconds. Before you run it, what do you think will happen?