Arduino Tutorial

What is arduino ?

Arduino is an open-source electronics prototyping platform based on flexible, easy-to use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. The microcontroller on the board is programmed using the Arduino programming language and the Arduino Development Environment. Arduino projects can be stand-alone, or they can communicate with software running on a computer.

There are plenty of other microcontrollers available. So you may be asking, why choose the Arduino? Arduino really simplifies the process of building projects on a microcontroller making it a great platform for amateurs. You can easily start working on one with no previous electronics experience.

In addition to Arduino’s simplicity, it is also inexpensive, cross-platform and open source. The Arduino is based on  Atmel’s ATMEGA328 and ATMEGA168 microcontrollers.

Arduino Software

Software programs, called sketches, are created on a computer using the Arduino integrated development environment (IDE). The IDE enables you to write and edit code and convert this code into instructions that Arduino hardware understands.
The IDE also transfers those instructions to the Arduino board (a process called uploading).

Adruino

Figure 1: Arduino Uno

 

Arduino Hardware

The Arduino board is where the code you write is executed. The board can only control and respond to electricity, so specific components are attached to it to enable it to interact with the real world. These components can be sensors, which convert some aspect of the physical world to electricity so that the board can sense it, or actuators, which get electricity from the board and convert it into something that changes the world. Examples of sensors include switches, accelerometers, and ultrasound distance sensors. Actuators are things like lights and LEDs, speakers, motors, and displays.

There have been a number of Arduino versions, all based on an 8-bit Atmel AVR reduced instruction set computer (RISC) microprocessor. The first board was based on the ATmega8 running at a clock speed of 16 MHz with 8 KB flash memory; later boards such as the Arduino NG plus and the Diecimila (Italian for 10,000) used the
ATmega168 with 16 KB flash memory. The most recent Arduino versions, Duemilanove and Uno, use the ATmega328 with 32 KB flash memory and can switch automatically between USB and DC power. For projects requiring more I/O and memory, there’s the Arduino Mega1280 with 128 KB memory or the more recent Arduino Mega2560 with 256 KB memory.
The boards have 14 digital pins, each of which can be set as either an input or output, and six analog inputs. In addition, six of the digital pins can be programmed to provide a pulse width modulation (PWM) analog output. A variety of communication protocols are available, including serial, serial peripheral interface bus (SPI), and I2C/ TWI. Included on each board as standard features are an in-circuit serial programming (ICSP) header and reset button.

 

 Project 1: blinking LEDs 
Let’s put some LEDs and resistors to work. In this project, we’ll use five LEDs to create a kind of wavelike light pattern.

The Hardware
Here’s what you’ll need to create this project:

>>    Five LEDs
>>    Five 560W resistors
>>    One breadboard
>>   Various connecting wires
>>   Arduino and USB cable

We will connect the LEDs to digital pins 2 through 6 via the 560-ohm
current-limiting resistors.

The Sketch
Now for our sketch. Enter this code into the IDE:

// Project 1 - Creating a Blinking LED Wave

 void setup()
{
  pinMode(2, OUTPUT); // LED 1 control pin is set up as an output
  pinMode(3, OUTPUT); // same for LED 2 to LED 5
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}
 void loop()
{
  digitalWrite(2, HIGH);  // Turn LED 1 on
  delay(500);             // wait half a second
  digitalWrite(2, LOW);   // Turn LED 1 off
  digitalWrite(3, HIGH);  // and repeat for LED 2 to 5
  delay(500);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  delay(500);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  delay(500);
  digitalWrite(6, LOW);
  digitalWrite(5, HIGH);
  delay(500);
  digitalWrite(5, LOW);
  digitalWrite(4, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(3, HIGH);
  delay(500);
  digitalWrite(3, LOW);
  // the loop() will now loop around and start from the top again
}

In void setup() the digital I/O pins are set to outputs, because we want them to send current to the LEDs on demand. We specify when to turn on each LED using the digitalWrite() function in the void loop() section of the sketch.
The Schematic
Now let’s build the circuit. Circuit layout can be described in several ways. For the first few projects, we’ll use physical layout diagrams similar to the one shown in Figure 2.
By comparing the wiring diagram to the functions in the sketch, you can begin to make sense of the circuit. For example, when we use  digitalWrite(2, HIGH), a high voltage of 5 V flows from digital pin 2, through
the current-limiting resistor, through the LED via the anode and then the cathode, and finally back to the Arduino’s GND socket to complete the circuit. Then, when we use digitalWrite(2, LOW), the current stops and the LED turns
off.
cct1
Figure 2: Circuit layout for project 1
Running the Sketch
Now connect your Arduino and upload the sketch. After a second or two, the LEDs should blink from left to right and then back again.
Project 2: Using for loops

When designing a sketch, you’ll often repeat the same function. You could simply copy and paste the function to duplicate it in a sketch, but that’s inefficient and a waste of your Arduino’s program memory.Instead, you can use for loops. The benefit of using a for loop is that you can determine how many times the code inside the loop will repeat.To see how a for loop works, enter the following code as a new sketch:

// Project 2 - Repeating with for Loops

int d = 100;

void setup()
{
   for(int a = 2; a < 7; a++)
         pinMode (a, OUTPUT);
}

void loop()
{
      for (int b = 2; b < 7 ; b++)
  {
    digitalWrite(b,HIGH);
    delay(d);
    digitalWrite(b,LOW);
    delay(d);
  }
}

The for loop will repeat the code within the curly brackets beneath it as long as some condition is true. Here, we have used two new integer variables, a and b, which starts with the value 2. Every time the code is executed, the a++ will add 1 to the value of a. The loop will continue in this fashion while the value of a is less than 7 (the condition). Once it is equal to or greater than 7, the Arduino moves on and continues with whatever code comes after the for loop. The number of loops that a for loop executes can also be set by counting down from a higher number to a lower number. To demonstrate this, add the following loop to the Project 2 sketch after the second for loop:

for (int b = 5 ; b > 1 ; b--)
  {
    digitalWrite(b, HIGH);
    delay(d);
    digitalWrite(b, LOW);
    delay(d);
}

 

Pulse-Width Modulation(PWM) – Varying led Brightness

Rather than just turning LEDs on and off rapidly using digitalWrite(), we can define the level of brightness of an LED by adjusting the amount of time between each LED’s on and off states using pulse-width modulation (PWM). PWM can be used to create the illusion of an LED being on at different levels of brightness by turning the LED on and off rapidly, at around 500 cycles per second. The brightness we perceive is determined by the amount of time the digital output pin is on versus the amount of time it is off—that is, every time the LED is lit or unlit. Because our eyes can’t see flickers faster than 50 cycles per second, the LED appears to have a constant brightness. The greater the duty cycle (the longer the pin is on compared to off in each cycle), the greater the perceived brightness of the LED connected to the digital output pin.

The greater the duty cycle (the longer the pin is on compared to off in each cycle), the greater the perceived brightness of the LED connected to the digital output pin.

Figure 3 shows various PWM duty cycles. The filled-in gray areas represent the amount of time that the light is on.
As you can see, the amount of time per cycle that the light is on increases with the duty cycle.

pwm

Figure 3: Various PWM duty cycles

 Only digital pins 3, 5, 6, 9, 10, and 11 on a regular Arduino board can be used for PWM. They are marked on the Arduino board with a tilde (~),
To create a PWM signal, we use the function analogWrite(x, y), where x is the digital pin and y is a value for the duty cycle, between 0 and 255, where 0 indicates a 0 percent duty cycle and 255 indicates 100 percent duty cycle.

Project 3: PWM Example

Now let’s try this with our circuit from Project 2. Enter the following sketch into the IDE and upload it to the Arduino:

// Project 3 - Demonstrating PWM

int d = 5;

void setup()
{
  pinMode(3, OUTPUT);    // LED control pin is 3, a PWM capable pin
}

void loop()
{
  for (int a = 0 ; a < 256 ; a++)           
        {               
           analogWrite(3, a);               
           delay(d);           
        }           

 for (int a = 255 ; a >= 0 ; a--)
       {
         analogWrite(3, a);
         delay(d);
       }
         delay(200);
}

The LED on digital pin 3 will exhibit a “breathing effect” as the duty cycle increases and decreases. In other words, the LED will turn on, increasing in brightness until fully lit, and then reverse. Experiment with the sketch and circuit. For example, make five LEDs breathe at once, or have them do so sequentially.

 Project 4: Using a switch as digital input

Our goal in this project is to create a button that turns on an LED for half a second when pressed.

The Algorithm

Here is our algorithm:

1. Test to see if the button has been pressed.

2. If the button has been pressed, then turn on the LED for half a second,
and then turn it off.
3. If the button has not been pressed, then do nothing.
4. Repeat indefinitely.

 

The Hardware

Here’s what you’ll need to create this project:
• One push button
• One LED
• One 560 W resistor
• One 10 kW resistor
• One 100 nF capacitor
• Various connecting wires
• One breadboard
• Arduino and USB cable

The Schematic
First we create the circuit on the breadboard with the schematic shown in Figure 4. Notice how the 10 kW resistor is connected between GND and digital pin seven. We call this a pull-down resistor, because it pulls the voltage at the digital pin almost to zero. Furthermore, by adding a 100 nF capacitor across the 10 kW resistor, we create a simple debounce circuit to help filter out the switch bounce. When the button is pressed, the digital pin goes immediately to high. But when the button is released, digital pin seven is pulled down to GND via the 10 kW resistor, and the 100nF capacitor creates a small delay. This effectively covers up the bouncing pulses by slowing down the voltage falling to
GND, thereby eliminating most of the false readings due to floating voltage and erratic button behavior.

pro4

Figure 4: Schematic for Project 4

 

The Sketch

For the sketch, enter and upload the code below:

// Project 4 - Demonstrating a Digital Input

 #define LED    12 
#define BUTTON 7

void setup()
{
   pinMode(LED, OUTPUT);         // output for the LED
  pinMode(BUTTON, INPUT);        // input for the button
}

void loop()
{
  if (digitalRead(BUTTON) == HIGH)
  {
      digitalWrite(LED, HIGH);   // turn on the LED
      delay(500);                // wait for 0.5 seconds
      digitalWrite(LED, LOW);    // turn off the LED
  }
}

Leave a Reply