What Is a Microcontroller? - Programming an Arduino Board
What is a Microcontroller?
You probably have lots of these in your home although you don't know it! A microcontroller is a type of integrated circuit (IC) or "chip" which is often used in devices in your home which incorporate electronic circuitry. You have probably heard of microprocessors, e.g. Intel Pentium, and these are used as the CPU (Central Processing Unit), effectively the "brain", in a computer. Microprocessors are integrated circuits which process the instructions in a computer program, perform calculations and send data to and from memory and disk. A microcontroller is a specific type of microprocessor. It differs from a normal microprocessor in the following ways:
- It has built in "I/O" (input/output) capabilities. So it can read and write digital and analog values/states, and connect directly to the "real world". A microcontroller, unlike a microprocessor can connect directly to switches, buttons, LCD displays, LEDS, relays and serial ports
- Microcontrollers are generally used for low to medium complexity, specific tasks in equipment. This contrasts with the powerful, number crunching microprocessors used in PCs which handle a variety of software applications.
- Microcontrollers are often used in portable devices which run on batteries, e.g. digital cameras. So they are often low powered with a small current consumption (unlike the heat sinked, fan cooled microprocessor in a desktop computer)
- Compared to the microprocessor in a PC, the RAM within a microcontroller can typically range from 64k down to as little as 1k
- The program in a microcontroller is usually stored in EPROM or EEPROM. This is a type of non volatile (program doesn't disappear when the device is turned off) memory which can be continuously wiped and rewritten.
What are Microcontrollers Used For?
Lots of devices make use of microcontrollers. Some examples:
- Burglar alarms incorporate a microcontroller chip which is connected to the keypad, display and sensor/contact inputs. Microcontrollers are generally self contained chips with the ALU (Arithmetic Logic Unit), memory and I/O all contained within one integrated circuit
- Older automatic washing machines used a cam switch for sequencing the operations during a wash cycle. This was quite a complex switch and was mounted on the end of the shaft of the knob you used to select a wash program. Newer machines use a microcontroller to sequence operations. Other appliances such as microwave ovens and dishwashers may incorporate a microcontroller
- TVs use microcontrollers to handle the selection of channels and reading the state of buttons on the TV
- Microcontrollers are used for engine control and display of information on the dashboard (fascia) of vehicles
- Digital cameras use microcontrollers to handle input from buttons, control of image capture and display.
Types of Microcontrollers
Some commonly used microcontrollers:
- Zilog Z8
- Intel 8051
- Texax Instruments TIMSP430
- Atmel AVR
What is an Arduino?
Arduino is an open source hardware/software programming platform based around Atmel microcontrollers. Open source means that circuit schematics and source code of software used in designs is freely available and can be modified by enthusiasts. Arduino development boards with their analog and digital, inputs and outputs, are ideal for artists, designers and electronic hobbyists who wish to put together a system without having to know a huge amount about digital design. Input and output signals are made available on the Arduino board using rows of female connectors into which individual leads or single in line (SIL) connectors can be plugged.
An Arduino development board will have at least:
- 9 digital pins which can be either input/output channels. Some of these can be setup as PWM (Pulse Width Modulation) outputs. A PWM signal is a square wave whose pulse width can be varied. PWM is used for speed and position control of motors and servos in robotics and remote control applications
- 4 analog input channels.
- At least one serial port which may also be used for download of code to the Arduino
Some boards also have a range of analog output channels.
There is a high degree of flexibility as regards the function of pins, some of which can be configured as either analog or digital. Digital pins can be configured as either input or output.
Arduino boards sense the environment around them as they receive input from sensors connected to these analog and digital inputs. They can also control actuators such as motors, alarm sounders and electric valves, or switch on LEDS, lamps or other visual indication devices. The output drive capability of outputs is limited, so usually transistors, FETS, or relays must be used between the Arduino output pin and driven device. An output is capable however, of driving LEDs directly.
The connectors on board are arranged in a standard manner so that shields can be connected. Shields are modules with a dedicated function (e,g, infra-red output, bluetooth, GSM, WIFI, Ethernet, motor on/off control using relas, stepper motor control). Depending on the shield type, it may be possible to stack several shields on top of each other and address them individually over an I2C, serial bus.
Since the Arduino code and hardware is open source, third party developers have produced clones of the official Arduino boards.
Arduino boards have from 32 to 512k of flash memory which can be used for program storage
This is the first Leonardo board I started using. It has a modest amount of I/O including 20 digital I/O ports, 7 PWM channels, 12 12 bit analog input channels and 5 analog output channels. Headers are soldered onto the board, but you may be able to find a version without them if you need more space.
What Can an Arduino Board Be Used For?
An Arduino board can be used for doing lots of cool stuff. You can easily add LCD displays, a keypad or seven segment display modules and build a simple system quickly. Lots of information and libraries are available on the Arduino.cc website. You can design your own add on electronics or use shields to build a modular system. Arduinos because of their versatility can be used for simple, mundane tasks or complex applications. An example of a simple application would be the ability to turn on a device when the temperature in a room falls below a certain level. It wouldn't be too difficult to extend this application, and add a GSM module which could send out a text message when this occurs. Arduino boards can be used to implement a complex home automation system which could be controlled over a smart phone. They are also used by enthusiasts as the processing and control device in robots, and kits are available for building a simple wheeled robot.
Programming an Arduino Board
Arduino boards are programmed in C and high level functions are provided as standard for reading and writing to analog and digital pins and serial ports. Source code is known as a "sketch". Standard libraries are also available for tasks such as output to an LCD panel or communication with a GSM module. Lots of other libraries and code have been contributed by enthusiasts and are available on the Arduino website.
A basic code editor/compiler is available on the Arduino website which can be used for developing programs. However the editor is basically just like Wordpad and doesn't provide any color coded highlighting or have sophisticated debug features. Atmel Studio, a cut-down, customized version of Visual Studio is an alternative which provides these features.
Programs are downloaded onto an Arduino board using either a serial port or USB connection. A bootloader then loads the program into memory on reset so that an external programmer isn't required.
Simple Arduino Sketch
A simple sketch written in C which can be run on an Arduino board
// Simple program to read a character from the serial port, echo it back to the remote terminal and flash an LED
Serial.begin(9600) // Open the port at 9600 baud
pinMode(13,OUTPUT) // Set pin 13 as an output
if(Serial.available()) // if there is data in the receive buffer.....
character = Serial.read(); // read the character
Serial.write(character); // echo the character to the serial port
// Flash an LED
digitalWrite(13,HIGH); // turn on the LED connected to pin 13
delay(50) // delay 50 mS
digitalWrite(13,LOW); // turn off the LED connected to pin 13
© 2014 Eugene Brennan