ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to Write C++ Programs for Arduino

Updated on February 21, 2013

What Is Arduino

Arduino is an open source hardware platform for artists, inventors and tinkerers. For about fifty dollars you can design electronic devices that can do almost anything. Arduino is a circuit board that can:

  • Be programmed
  • Take input from sensors
  • Produce output to actuators

Arduino systems can be programmed to do almost anything depending on the sensors and actuators that they are attached to. A sensor takes information from the environment and inputs it into the electronic device. An actuator takes a signal from the electronic device and alters the devices environment in some way.

Examples of sensors are:

  • Computer keyboard
  • Photoelectric sensors
  • Temperature sensors
  • Motion sensors
  • Network connections

Examples of actuators are:

  • Network connections
  • Lights
  • Motors
  • Display screens

Getting Started With Arduino

Getting started with this system is very simple. You can purchase the Arduino kit which contains the circuit board or you can put together your own circuit board matching the open source hardware specification. You can then find electronic junk. Old toys can be sources of sensors or actuators. Microphones can send electric signals to the mother board when you make sounds and this will allow the system to respond to sounds you create. You can buy sensors and actuators from hobby shops. Getting started is simple and it only requires:

  1. Get the circuit board
  2. Get lights or some other actuator
  3. Get a keyboard or some other sensor
  4. Hook them together
  5. Create a program also known as a sketch

Programming Arduino

You can program your circuit board with c or you can use a c variant that ships with Arduino. If you use c all you need to do is get the arduino c library and include it using a simple #include directive and you will have access to the various pins for output and input. The c variant is even easier to use as you don't have to include the c library or set up a main function.

To make it easy to program, the board comes with the following:

  • USB port and controller for easy interfacing with your computer
  • Pins on the circuit board for input and output from sensors and actuators

The Processing development environment is used to write, compile, and upload programs or sketches to the circuit board. It is an open source system and you can download the IDE for Linux, Windows, and Mac. It is based on C so it is a concise and fast language.

Arduino uses the avr microprocessor. The above link is to the manual that tells you what functions in the library are available to you. Download the pdf and look through it. Examples of what you can do with it include send hi or low voltages to specific pins. This can be used to trigger lights being turned on or off, or motors being activated or deactivated. For this you would include avr/io.h header in your code to use the input and output functionality. This header file always includes the portpins header file so you can write commands such as PORTB = 0x020; This will send a "high" or "On" signal to the port.

Arduino Help

There is a large "Maker" community that is there to both help you when you fall and praise you when you shine. You can ask questions in arduino groups as well as post your completed projects to those groups.

This is just one of many groups that exist to help you in your journey through the world of Arduino. Create interesting things with electronic items around the house that you would have thrown away. Learn about what is possible with these things along the way.

How To Use The Arduino Language

You can use the language that comes installed in the Arduino IDE. This language is based on C. It has two main functions where you do all your coding. The setup function is where you set up the variables and constants for your board. The loop function is where you tell the board to do something over and over again. It is where you provide most of the functionality for your program.

When you write something like

void loop
	digitalwrite(Actuator, HIGH);

You turn on the Actuator. Actually this particular command, though it is simple, it is too simple. You would continuously send High signals to the port with the actuator. You would keep turning it on. You could add a delay command to turn it off, and then turn it back on again, but you get the picture of how to do it.

If you haven't already, go through the video and have fun playing with your Arduino.


    0 of 8192 characters used
    Post Comment

    No comments yet.