Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

programming: connecting with pins

If you look at the Arno’s microcontroller chip, you will see pins sticking out of all four sides.  These pins connect the chip to the rest of the circuit.  Some of the pins have special functions, like connecting to the power supply or USB communication.  The rest of the pins have input/output (I/O) functions.  If these pins are set as inputs, they read values from other components like sensors, buttons, or a joystick. If they’re set as outputs, they can control devices; they might light an LED or run another chip that can drive a larger load like a motor.  This is the real power of Arduino: the ability to look at inputs, make some decisions, and then control outputs. 

 We access these pins according to their pin number.  The pin numbers for the Arno board are listed in the Pin Key. In our projects, we use variables to refer to the pin numbers.  For the sake of consistency, we use the same variable names in each sketch when we’re referring to a particular pin. In some cases, we have to use different pin numbers to refer to the same actual pin depending on whether we’re using it as an analog input or a digital output. For example, the piezo element’s pin number is 12 when we’re using it as a digital output or A11 when we’re using it as an analog input.

  In the setup() block of our sketch, we need to tell the Arduino whether we want a pin to function as input or output.  We use the pinMode statement:

pinMode(4,INPUT);  // set pin 4 as an input

or

pinMode(4,OUTPUT); //set pin 4 as an output

We can also set pinMode using a variable to refer to the pin number:

int LED1 = 13;

pinMode(LED1,OUTPUT);

Before we go further with pins, we need to look at the differences between digital and analog signals.

digital and analog: highs, lows, and in-between

The world of electronics can be divided into a digital side and an analog side.  Computers see the world as digital: everything eventually needs to be converted to 1’s and 0’s.  In some cases, a single bit can represent a switch.  Either it’s off (0) or it’s on (1).  In other cases, we use bits to represent a range of values.  For example, with two bits, we can represent four values: 00, 01, 10, and 11.  We can represent more values using more bits (remember the byte, int, and long variables).  This might not make sense right away, but let’s continue.

An Arduino can read inputs from the outside world using statements like this:

pinMode(4, INPUT);

digitalRead(4);

This command tells the Arduino to read the value of pin 4 as either LOW (off) or HIGH (on).  What do we mean by low or high?  It’s about electrical potential.  If the voltage is below 3V it’s considered low, otherwise it’s considered high.  We might use this command to read a push button.  Normally, the pin is attached to the positive side of the battery (through something called a pullup resistor) and the Arduino reads it as HIGH (go back and take a look at Circuit 2 on the Arno board).  When the button is pushed, it is read as LOW.  We will use this type of reading in our projects.  

We can also use pins as outputs.  In this case we would use a statement like:

pinMode(6,OUTPUT);

digitalWrite(6,HIGH);

Now we’re telling the Arno to set pin 6 HIGH, which means it will have the same voltage as the input voltage (5V).  We can use this voltage to do tasks that don’t require much current, like powering an LED (e.g., Circuit 1 on the Arno board).  We need to be careful that we don’t create a low resistance path to ground, though.  Each pin should not “source” (act as a positive terminal) or “sink” (act as a negative terminal) more than 40 milliamps.  If we want the Arno to act as a sink rather than a source, we would change the command to:

digitalWrite(6,LOW);

Now you’re probably asking the question: it’s fine to turn things on and off, but what if we want to read or write something that’s not simply on or off but in between? Now we’re talking about analog signals.  This doesn’t come as naturally to the Arno as the digital side.  After all, computers see the world as just a bunch of 1’s and 0’s.  The microcontroller on the Arno has a special device called an analog-digital converter (ADC) to measure voltage.  There are pins on the Arno that are tied into the ADC and can read analog signals using a statement like:

analogRead(A0);

The analogRead statement can read voltages between 0 and 5 V (the voltage of the chip).  It does this by converting the voltage into a digital number it can handle.  The ADC uses 10 bits and returns numbers between 0 and 1023 (that’s the range of 210).   If you connected a wire between pin A0 and ground, you would get a reading of 0.  If you connected it to the 5 V power supply, you would get a reading of 1023.  If you have a device that creates a voltage of 2.5V, you would get a reading of 512.  We will use the analogRead statement to read values in several of our circuits.  An example is Circuit 7,  the thumbwheel potentiometer.

What if we want to output an analog signal?  This is more difficult.  Computers have a hard time outputting a voltage other than ground or the supply voltage.  In this case, the Arduino fakes it using an approach called pulse width modulation (PWM).  Instead of outputting 2.5V, the Arduino rapidly switches a 5 V supply on and off so it is one 50% of the time. The switching speed in about 490 times per second. We can use the PWM signal to make an LED light dimly, brightly, or in between.  With some additional circuitry, we can use PWM to control a motor speed or operate other devices that we don’t want to be only on or off.  We can access PWM on special pins using a command like:

analogWrite(4,100);

This means to pulse pin 4.  The value can range from 0 to 255 (PWM has a resolution of 8 bits).  A value of 100 would leave the pin on 100/255 = 39% of the time.
next: flow control

Copyright Olympia Circuits LLC 2014. All Rights Reserved.