Arduino Coding Session

Today, we will run through the basics of Arduino programming. To follow along, please go to the reference section of the Arduino site and step through the functions as we go.

Also, download the files below. They are the Arduino code examples we'll follow once we start making real circuits.

knight_rider_1.zip471 bytes
knight_rider_2.zip39.57 KB
vibrapot.zip37.68 KB


  • Voltage and Logic
  • Digital "Voltage"
    • LOW is 0V
    • HIGH is 5V
  • Analog "Voltage"
    • Voltage in 1024 divisions between 0 and 5V

Data Types

  • numbers
    • byte 0 - 255
      • space saving! use this when possible!
    • int
      • handy for negative numbers and most other uses bigger than a byte
      • -32,768 to 32,767
    • long
      • -2,147,483,648 to 2,147,483,647
      • Four times bigger than a byte
      • Do you REALLY need numbers this size? Only use this when necessary
  • char
    • also a byte
    • but you can use ASCII characters instead of numbers
    • google for "ASCII table" to see what number is what character
    • Handy to know for sensors and other stuff that uses serial. Bytes = chars.
  • arrays
    • an array is a kind of list of other data types
    • can be any data type
    • can be set to be whatever size you need
    • arrays are numbered starting from 0
    • Let's play with this

Control structures

  • if/else
    • simple branching
  • switch case
    • more complicate branching
    • for multiple cases
    • like if the input is 1, go to first base, 2 go to second base, 3 go to third, 4 home run, default strike out
    • after each case, be sure to add break; otherwise, it'll keep on going
  • for loop
    • good for loop a set number of times
    • like looping through all the pins
  • while
    • good for loop an unknown number of times
    • maybe for forever
    • maybe until something happens to break out

Tutorial 1: Data and Serial

Arduino Hooked UpArduino Hooked UpNow we'll run the knight_rider_1 tutorial. Open up the files in the zip archive. All you need for this is to plug in the Arduino to your computer.

Once the board is plugged in, hit reset on the board, then click the Upload to I/O icon on the Arduino interface. Once that's done, click the Serial Monitor icon to see the numbers move up and down. Now you're talking (to the Arduino!)

Digital Read/Write

  • pinMode
    • takes the pin to be determined
    • takes the mode -> in or out
    • MUST do this for most pins. Forgetting this will get you in trouble
  • digitalWrite
    • takes pin and sets it HIGH or LOW
    • that is, gives that pin 0 or 5V
  • digitalRead
    • takes pin and returns HIGH or LOW

Other Cool Stuff

  • Math
    • max, min, contrain, randomSeed, random
  • Time
    • delay
      • important for adding pauses
      • otherwise things will run crazy fast

Tutorial 2: Digital Read and Write

Knight LightsKnight LightsNow let's put some of this stuff together into a new circuit. You'll need four LEDs, four 220 ohm or 470 ohm resistors, one 1K ohm resistor, a push button, and a bunch of wires.

Hook four wires into the Arduino's digital outputs (check the code to see which ones.) Connect the smaller resistors to each of those wires, then connect an LED to each resistor with its short leg to ground. Be sure to hook up the 5V and GND lines between your Arduino and breadboard.

Connect the 1K resistor to ground (this is a pull down) and to one pole of the switch. Hook that same pole to the input on the arduino. Hook the other pole to the 5V source.

You're all set!

Analog Read/Write

  • Analog pins don't require pinMode -- they are fixed
  • analog read pins are on a separate set up pins
  • analogue write pins (PWM pins) are on 9, 10, and 11
  • analogWrite takes a pin and a value (but the value must be between 0 and 255)
  • analogRead takes a pin (must be numbered from the analog pins) and returns a value 0 - 1023
    • This is four times larger than analogWrite
    • The number corresponds with the voltage
      • 0 is 0V
      • 1023 is 5V
    • You can test your sensors and see what voltage that give off with a multimeter, then build your circuit around that
    • or check the output from serial and calibrate your application that way

Tutorial 3: Fun With Pot and Vibrators

Buzzy BasicsBuzzy BasicsOkay, actually, that's a pot as in potentiometer. Nyuk, nyuk. Anyway, let have some fun here!

  • Connect pot to analog in pin
  • Connect vibrator to PWM out pin
  • Get the input for the analog in
  • Scale the input
    • Huh?
    • Scaling adjusts the input values of 0 - 1024 to a different range that we can use
    • In this case, we want the output to be from 0 to 127
  • write the output to the PWM pin
  • buzzzZZZZZzzzzZZZZZzzz!

Advanced Stuff

  • shiftOut
    • turns numbers into streams of data
    • handy for working with other chips
    • especially simple ones that don't take serial
  • pulseIn
    • Can measure pulses sent in by simple devices and chips
    • helpful for timing