Introduction To Arduino
Introduction To Arduino
What is Arduino?
Arduino is a tool for making computers that can sense and control more of the physical world than
your desktop computer. It's an open-source physical computing platform based on a simple
microcontroller board, and a development environment for writing software for the board.
Arduino can be used to develop interactive objects, taking inputs from a variety of switches or
sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects can
be stand-alone, or they can be communicating with software running on your computer (e.g. Flash,
Processing and MaxMSP.) The boards can be assembled by hand or purchased preassembled;
the open-source IDE can be downloaded for free.
Why Arduino?
There are many other microcontrollers and microcontroller platforms available for physical
computing. Parallax Basic Stamp, Netmedia's BX-24, Phidgets, MIT's Handyboard, and many
others offer similar functionality. All of these tools take the messy details of microcontroller
programming and wrap it up in an easy-to-use package. Arduino also simplifies the process of
working with microcontrollers, but it offers some advantage for teachers, students, and interested
amateurs over other systems:
Cross-platform - The Arduino software runs on Windows, Macintosh OSX, and Linux
operating systems. Most microcontroller systems are limited to Windows.
Open source and extensible software- The Arduino software and is published as open
source tools, available for extension by experienced programmers. The language can be
expanded through C++ libraries, and people wanting to understand the technical details can
make the leap from Arduino to the AVR C programming language on which it's based.
Similarly, you can add AVR-C code directly into your Arduino programs if you want to.
Open source and extensible hardware - The Arduino is based on Atmel's ATMEGA8 and
ATMEGA168 microcontrollers. The plans for the modules are published under a Creative
Commons license, so experienced circuit designers can make their own version of the
module, extending it and improving it. Even relatively inexperienced users can build the
breadboard version of the module in order to understand how it works and save money.
Introduction
Photocells
Photoresistors, LDR (light dependent resistor)..
What is a photocell?
Photocells are sensors that allow you to detect light. They are small, inexpensive, low-power,
easy to use and don't wear out. For that reason they often appear in toys, gadgets and
appliances. They are often referred to as CdS cells (they are made of Cadmium-Sulfide), light-
dependent resistors (LDR), and photoresistors.
Photocells are basically a resistor that changes its resistive value (in ohms Ω) depending on how
much light is shining onto the squiggly face.
The easiest way to measure a resistive sensor is to connect one end to Power and the other to
a pull-down resistor to ground. Then the point between the fixed pulldown resistor and the
variable photocell resistor is connected to the analog input of a microcontroller such as an
Arduino (shown)
Vo = Vcc ( R / (R + Photocell) )
That is, the voltage is proportional to the inverse of the photocell resistance which is, in turn,
inversely proportional to light levels
Interrupts
An interrupt is a signal to the processor emitted by hardware or software indicating an
event that needs immediate attention. An interrupt alerts the processor to a high-priority
condition requiring the interruption of the current code the processor is executing. The
processor responds by suspending its current activities, saving its state, and executing a
small program called an interrupt handler (or interrupt service routine, ISR) to deal with
the event. This interruption is temporary, and after the interrupt handler finishes, the
processor resumes execution of the previous thread. There are two types of interrupts:
hardware interrupts and software interrupts.
Hardware interrupts
A hardware interrupt is an electronic alerting signal sent to the processor from an
external device. Arduino uno board has two external interrupts: int.0 (on digital pin 2) and
int.1 (on digital pin 3). when the triggering event is captured at these pins, the
corresponding interrupt service routine is executed.
Syntax:
Parameters:
function: the function to call when the interrupt occurs; this function must
take no parameters and return nothing. This function is
sometimes referred to as an interrupt service routine.
mode: defines when the interrupt should be triggered. Four contstants
are predefined as valid values:
NOTE:
You should declare as volatile any variables that you modify within the attached
function.
Software interrupts
A software interrupt is caused either by an exceptional condition in the processor
itself, or a special instruction in the instruction set which causes an interrupt when it is
executed. In this experiment we will focus on the interrupt caused by timer1 overflow.
Timer1 is 16 bit hardware timer on the ATmega168/328. There are 3 hardware timers
available on the chip, and they can be configured in a variety of ways to achieve different
functionality. The timer can be programmed by some special registers. You can configure
the prescaler for the timer, or the mode of operation and many other things. Timer1's
clock speed is defined by setting the prescaler, or divisor. This prescale can be set to 1,
8, 64, 256 or 1024.
Timer Register
You can change the Timer behaviour through the timer register. The most important
timer registers are:
d
TCCRx - Timer/Counter Control Register. The prescaler can be configured here.
TCNTx - Timer/Counter Register. The actual timer value is stored here.
Different clock sources can be selected for each timer independently. To calculate the
timer frequency (for example 2Hz using timer1) you will need:
This sketch will take the analog voltage reading and use that to determine how bright the red LED
is. The darker it is, the brighter the LED will be! Remember that the LED has to be connected to a
PWM pin for this to work, I use pin 11 in this example.
Connect one end of the photocell to 5V, the other end to Analog
0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 11 through a resistor to ground
For more information see www.ladyada.net/learn/sensors/cds.html
*/
int photocellPin = 0; // the cell and 10K pulldown are
connected to a0
int photocellReading; // the analog reading from the sensor
divider
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
delay(10000);
}
Serial Monitor:
Hardware interrupts
1- Connect a push button to digital pin 2 in the arduino uno board.
2- Copy the following code to the arduino IDE then compile and upload it to the
board
void blink()
{
state = !state;
}
3- Explain how the previous code works.
Software interrupts
1. Copy the following code to the arduino IDE then compile and upload it to the
board
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
void loop()
{
// your program here...
}
To Do (PWM)
Write a code to generate a square wave on the pin 13 with frequency 1 KHz and
duty cycle that is controllable by a potentiometer.