June 20, 2018

Microcontrolling on the Cheap With Arduino and Linux - page 2

Open, Affordable Microcontrollers

  • October 7, 2008
  • By Rob Reilly
Setting up the board is pretty simple. You connect your switches and sensors to the inputs, connect your LEDs and loads to the outputs, then plug in the USB cable. Graphic #1 shows a picture of my hardware setup. Start up the Arduino programming environment from the command line with:

���rreilly> ./arduino

See graphic #2 for a screen shot of the Arduino programming GUI.

Open the Sketchbook under the File tab and you'll find a bunch of example programs that you can use as foundations for your own projects.

I lifted this simple LED blink program, from the Arduino sketchbook example directory and added a few of my own modifications.

���� * AnalogInput
���� * by DojoDave
���� *
���� * Turns on and off a light emitting diode(LED) connected to digital
���� * pin 13. The amount of time the LED will be on and off depends on
���� * the value obtained by analogRead(). In the easiest case we connect
���� * a potentiometer to analog pin 3.
���� *
���� * http://www.arduino.cc/en/Tutorial/AnalogInput
���� */

���� int potPin = 3; �����// select the input pin for the potentiometer
���� int ledPin = 13; ���// select the pin for the LED
���� int val = 0; �����������// variable to store the value coming from the sensor

�����void setup() {
���������� pinMode(ledPin, OUTPUT);��// declare the ledPin as an OUTPUT
���������� Serial.begin(9600);���������������// set up Serial library at 9600 bps

�����void loop() {
������� val = analogRead(potPin);������������ // read the value from the sensor
������� digitalWrite(ledPin, HIGH);����������� // turn the ledPin on
������� Serial.print("hello world! val = ");�// send the message to the serial line
������� Serial.println(val);��������������������������// send value of "val" to the serial line
������� delay(val);���������������������������������������// stop the program for some time
������� digitalWrite(ledPin, LOW);������������// turn the ledPin off
������� delay(val);���������������������������������������// stop the program for some time

The code is in a language called Processing and looks a lot like regular old C.

The Arduino editor allows standard copy, paste, and text manipulation. It also associates closing brackets and uses colored text for highlighting. The files are saved as plain text files with a .pde extension.

Compiling the program into a microcontroller binary is as simple as clicking on the Verify/Compile button under the Sketch tab. Once the code compiles, you can then click the Upload To I/O Board button on the main toolbar. New Arduino Diecimila boards don't do a board reset when you send over the new binary. My older NG model needs a quick push of the reset button (on the Arduino board), right before I click the download button on the GUI. Otherwise, I get a funky error and no download.

Once the program is downloaded, the Arduino will pause for about ten seconds, then start running the program. Workflow of making changes, compiling, and downloading is pretty fast after you do it a few times.

The code in the example reads a 10K Ohm potentiometer and flashes an LED at a rate that is determined by the value of that pot. Graphic #3 shows how I hooked up my Arduino for this example. I added in a couple of lines to send the age old Kernighan & Ritchie line "hello, world", along with the pot's value out to the serial line. The Arduino GUI has a handy little serial monitor on which you can watch the values streaming back from the microcontroller.

Naturally, you'll want to hook up temperature, light, and other types of sensors, as your experience with the Arduino grows. Motors, servos, solenoids, and other devices can be controlled on the outputs, using suitable circuits.

For a great introduction to microcontrollers in general, I recommend that you get Physical Computing, Sensing and Controlling the Physical World with Computers, by Dan O'Sullivan and Tom Igoe. This book is actually pre-Arduino and uses many of the older Stamp type microcontrollers. The companion Making Things Talk book, also by Tom Igoe, makes extensive use of Arduino modules and will take you into the world of RF, ethernet, WiFi, and Xbee communications. Tom is totally immersed in the Arduino technology and helped develop the boards.

Most Popular LinuxPlanet Stories

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.