Arduino basics
The basics
- this website is fundamental and you will be using it frequently: www.arduino.cc Links to an external site.
- there are many types of boards, but we will use Arduino Uno
Links to an external site.
- in particular, have at hand the page on 'Getting started with...' Links to an external site.
- download the programming environment Links to an external site. and have at hand the environment guide Links to an external site.
- Links to an external site.if you can't get it to work, see troubleshooting Links to an external site.
- for larger projects you can also use Arduino in Eclipse Links to an external site. (although this shouldn't be needed for this course)
- language reference Links to an external site.
- tutorials Links to an external site.
Programming Arduino
- the L led must blink at reset/power-up
- otherwise the board doesn't have a bootloader Links to an external site., so you can't program it
- connected to digital pin 13
- RX and TX LEDs blink during programming (and during serial communication)
- just one serial port
- same as the programming serial port
- also used for debugging
- connected to pins 0 (RX) and 1 (TX) and to USB
- you need to indicate in the environment
- the serial port you use (Tools/Serial Port)
- the type of Arduino board you use (Tools/Board)
- the Arduino language
- actually a C++ 'dialect'
- writing in the wrong places in memory may lead to an auto-reset, which is hard to detect
- procedural C syntax, similar to procedural Java syntax
- if you are familiar with Java/Processing, you can actually check the differences Links to an external site.
- programs are called "sketches"
- actually a C++ 'dialect'
- since most physical computing applications do a bit of setup then loop to check their sensors (polling), this is pre-programmed in Arduino
- void setup()
Links to an external site.
- setting pin modes (input, output) using pinMode() Links to an external site.
- setting the serial port using Serial.begin() Links to an external site.
- void loop() Links to an external site.
- void setup()
Links to an external site.
- like in any C++ program:
- you can define functions Links to an external site.
- you can use #define Links to an external site. for constants
- for, while, switch, etc.
- Libraries
Links to an external site. are available
- SoftwareSerial Links to an external site. does serial communication in software, on any pin
Debugging
- just do serial output, Serial.println()
Links to an external site. (don't forget Serial.begin()
Links to an external site. in setup() )
- but this means that you can't do both debugging and serial communication at the same time unless the other party is instructed to ignore
- the serial communication can be read with any other program, not just the Arduino environment. You can use the Windows Hyperterminal or the Mac Terminal
- just make sure you use the same baud rate in Serial.begin() and in your terminal program
- in the environment you can set the baud rate from a chooser
- on the Mac Terminal, you can use the screen utility (Ctrl-a k to stop)
- screen /dev/tty.usbmodemfa131 9600
Digital output
- pinMode Links to an external site.(pin, OUTPUT) followed by digitalWrite() Links to an external site.
- you can check the Blinking LED example (will open in the same tab)
- pin 13 has a resistor and is connected to the L LED so digital output on pin 13 doesn't need a LED
- the LEDs we have can actually be used without resistors and they'll blink brighter
Digital input
- digitalRead() Links to an external site.
- source code (will open in this tab)
- one can e.g. connect a IR motion detector
Links to an external site.
- white to digital pin, red to power, black to ground
- 220 Ohm between white and red
Analog input
- analogRead() Links to an external site.
- source code (will open in this tab)
- variable resistor between power and pin, another resistor between pin and ground
- you can use any Phidget analog sensor!
- uses an analog-digital converter (ADC) built in the microcontroller
- measures voltage
Analog output (PWM= Pulse Width Modulation)
- analogWrite() Links to an external site.
- can only be done on pins 3, 5, 6, 9, 10, 11
- dimming LED source code (will open in this tab)
- simple LED connection from pin 11 to ground seems to work
Driving servos
Interrupt-based programming
- you can define digital events on 2 pins (2 and 3) via interrupts
- not analog (give me an interrupt when value is under X)
- source code example (will open in this tab)
- attachInterrupt Links to an external site.(pin-2, function, mode)
- 'function' is a function name (should actually be &function, the address of the function in strict C language)
- 'mode' can be LOW, CHANGE, RISING, FALLING
- if mode is LOW, interrupts keep coming as long as the pins stays on LOW voltage
- pin-2: pin 2 -> 0, pin 3->1. Only values 0 and 1 (pins 2 and 3) work for interrupts
- detachInterrupt() Links to an external site.
- still, there is no event queue, no event details, no analog events, etc.
Other Arduino functions
- shiftOut(pin, clockPin, order, value) Links to an external site.writes a byte synchronously (with clock) in a shift register Links to an external site.
- unsigned long pulseIn(pin, value) Links to an external site.measures the length of a pulse waiting for Value (HIGH, or LOW)
- unsigned long millis() Links to an external site. tells the number of milliseconds that passed since the board started last
- delayMicroseconds(us) Links to an external site.finer than delay() Links to an external site.
- functions for random generation, use randomSeed() Links to an external site. first, then random() Links to an external site.
Debouncing
If you are reading this, it is probably because you have written code to run when you press a button, and you noticed that it is triggered multiple times even if you pressed only once. Or perhaps you did not realise that it is triggered several times, but you saw your actuators acting weirdly (and especially your LEDs!). This problem is called bouncing and it has a solution called... you guessed: Debouncing.
The Arduino website has a very nice explanation (Links to an external site.) on how to implement this solution.
Does this mechanism remind you of something? (Hint: Something from lab 1?)