Jump to content

Arduino projects HOWTO

Recommended Posts

On request from people seeing my Arduino gauges/controls in my build thread, i'll try to give an introduction to using these devices and then further detaill on my projects.
Arduinos are small microprocessor boards featuring Atmel processors and designed to be easily used by hobby/home users and learning aids. They connect to a PC with USB which allows programming and debugging via virtual serial port. They feature a number of 5V digital I/O pins, several analogue inputs (ADC) and some other hardware features such as SPI/I2C buses, interrupts etc.

The main Arduino page is here www.arduino.cc, you will find the IDE (integrated design envioment = program editor, compiler and programmer) its free with donations requested. You will also find documentation and user forums.
The program is made using a 'C' like code. If you have some prior experience in any language it should come quickly. Even if you dont i bet you can make an LED flash in a few minutes of playing and everything else builds from there.
To implement functions 'Libraries' are used, these are small programs that someone has written to do a specific task (and make life easier for you) Many standard libraries are included in the IDE, and many more are available to bolt-on to the IDE. For example i commonly use:
- u8glib - to talk to the LCD/OLED graphic displays
- ClickEncoder - reads a rotary encoder/switch which is a useful control device
- TinyGPS++ - reads data from GPS chipset and makes available for use
- and many more....

There are many peripheral boards available which they call 'shields' these can be plugged on top to add a whole array of other functions - Relays, Inputs, CAN Bus, Ethernet, Wifi, SD card logging... the list is endless.
You will find the boards and sheilds on:
- ebay (also some quite useful dev kits which include enough bits and sensors to get you going),
- Sparfun is a major seller https://www.sparkfun.com/
- Adafruit also has many useful parts https://www.adafruit.com/
There are a few flavours of the boards, one would normally chose depending on what you want to do with it - key choices are how many I/O pins you will need, how big and complex a program etc. I will talk about 3 of these flavours, the most common and useful models IMHO. Specs are on the linked pages below:

Arduino Nano - https://www.arduino.cc/en/Main/ArduinoBoardNano A very small board for those tiny projects.
Arduino Uno - https://www.arduino.cc/en/Main/ArduinoBoardUno The most useful board for most projects, a good one to learn with.
Arduino Mega - https://www.arduino.cc/en/Main/arduinoBoardMega When you need more I/O or program memory

The Uno and Mega have a compatible pin layout so the same shields can be used on both.

Some projects dont need a display (imagine a Nano just controlling a relay - you could do something like a intermittent was/wipe controller), but for most uses adding a graphic display is very useful. This could be an LCD or an OLED. I prefer OLED as they are very clear and have near 180 deg viewing angle. The library mentioned above (u8glib) will talk to almost any display you can find. I tend to use SPI protocol because it requires only 4 wire + 2 power to connect up the display.
My dash project uses these: ebay link, but have also used a smaller unit like this ebay link

Ok thats enough for one installment. I'll follow up with some example projects, and a post on each of my two dash displays.

Edited by geoffbeaumont
Fixing broken link (now doesn't go to bandsaw shop).
  • Like 3
Link to comment
Share on other sites


I suspect an Arduino will make it into my Moggy....

I suspect then, that we could easily get one to talk to Megasquirt as well? One of the canbus versions that is. Could make for a cheap transmission controller then.

Link to comment
Share on other sites

30 minutes ago, Bowie69 said:


I suspect an Arduino will make it into my Moggy....

I suspect then, that we could easily get one to talk to Megasquirt as well? One of the canbus versions that is. Could make for a cheap transmission controller then.

Yes you could easily make your own MS display / tweaker.

Link to comment
Share on other sites

There's a lot of resources on the Arduino site, and when you install the IDE you will also have a set of example programs in a folder.
But so you can get an idea of whats involved, heres a small sample program that comes with the IDE to flash an LED.

A program must have at least the following bits:
- A 'setup' function that will be run once when the board starts up. this is where you put all the stuff to initialize things and prepare for the main program.
- A 'loop' function. This is where you put the commands that you want to run during the program. The loop is run again and again until the device is turned of. A tip is to keep this as tidy as possible, if you have something complicated to do you would create a new function e.g void DoSomething(void) { .....} above setup, and then calling it from the main loop with DoSomething();

A few other tips to progress:
To include a library, typically add a line at the top e.g. #include <u8glib.h>
Where possible variables should be local (it saves memory) that is if they are used only inside a function, define them inside the function. If you must create a 'global variable' (one that can be used anywhere in the program) do it at the top of the code.
Constants can be made to make life easier. e.g in the example i could assign the value 13 to the text LEDPIN, in either of the following ways:
- #define LEDPIN 13
- const int LEDPIN 13;
and then in the code i could use a line like; digitalWrite(LEDPIN, LOW); which is easier to understand.

  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald

// the setup function runs once when you press reset or power the board 

void setup() {
    pinMode(13, OUTPUT); // initialize digital pin 13 as an output.
	// and put any other commands here that you want to run once on start-up

// the loop function runs over and over again forever

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second

so to make this work you would:
- Install the IDE
- start a new project, or open this example from the folder
- plug in your board via USB to your PC, check its detected and check there's a virtual COM port in windows devices, make a note of the Port number i.e. COM3
- select the COM port in the 'Tools' menu, and the board type in the same place
- Press the 'Upload' button on the menu bar, this compiles the program and uploads it.

You should now have your first working program

Link to comment
Share on other sites

4 hours ago, elbekko said:

How limited in size are you for programs? I was playing around with a little TI board a while ago, and the moment I tried using a library it ran out of memory...

They're not too bad, I've not found space a problem even on some bigger projects (like an embedded web server I wrote for one of my projects). My 3D printer uses an Arduino board to drive it and that's a pretty complicated and bulky piece of code. By comparison the compiled code I use on more traditional embedded controllers, MicroChip PIC for example, always takes up far too much room and I end up having to cull the code to try and make it fit - never had that issue with Arduino boards.

The biggest issue I find with Arduino in the mainstream/standard environment is that it drives you towards a simple sequential program whereas other systems generally encourage you to use a (much more efficient) interrupt driven architecture. In simple terms with Arduino, if you are monitoring a switch, for example, it is normal to execute a never ending loop and each time through you look at the state of the switch input to see if it's changed. In an event driven environment the hardware will generate an interrupt when the state of the switch changes so you can execute the code you want for that change at that point. It may seem trivial but the more complex your project and the more "random" the events you are looking for might be, the more of a limitation this lack of interrupt driven architecture becomes. Many people follow the "easy route" in Arduino and use the sequential system that most of the examples use and then find they have to relearn everything later when their project starts tying them in knots in massive if/else statements in the main loop.

If you look at the example LED flasher code above each time it runs through the loop it spends 2 seconds "delayed" during which time it's not doing anything else (it's actually running a timer interrupt using timer0). If you add any additional code, such as monitoring inputs or updating graphic displays then the delay between flashes can start to change, I know it's only an LED flashing but suppose you were trying to time something more accurately as you would need for a speedo, for example, or as in a recent project of mine, how long an injector is open for ?

A much better way to flash an LED (and monitor a switch, come to that) is to use a timer to generate an "interrupt" every second. An interrupt forces the processor to go off and execute a specific piece of your program regardless of what else it might be doing at the time, before returning to where it was. You can set interrupts based on timers and you can also set interrupts based on the state of an input pin changing. Just using those basic interrupts can allow you to accurately time events whilst at the same time respond to user inputs and there aren't many projects that I've worked on where some aspect of it doesn't involve timing something.

Link to comment
Share on other sites

Oh, good. I might have to get an Arduino at some point (or a Chinese clone) to play around with. I'm a .NET developer by trade, so doing low-level stuff has been a while :)

I did play around with interrupts on the TI thing, but it seemed very limited/didn't work very well... I remember it not wanting to give me a tick count from within an interrupt and things like that. It's a lot like the events I know and love, and I agree, locking the CPU with a sleep is just horrible.

Link to comment
Share on other sites

7 hours ago, zardos said:

I have an adafruit feather https://www.adafruit.com/categories/830
the M0 with wifi and oled shield ,its a bit more power and storage in a smaller package than most.

Nice little board if miniaturization is important, but bear in mind that you are limited to  shields made only for that board, not the wider range that will plug onto the standard format. It also it has no EEPROM, which can be useful in some projects.

If you are starting out and want that speed/memory benefit of the new version cpu, i would go with the Zero board as it maintains sheild compatibility. https://www.adafruit.com/products/2843

Link to comment
Share on other sites

Looking forward to reading this, I use PICs from time to time, I like them as they're just a chip you can stick on a board rather than having to house a mini computer somewhere, the problem is I often get a bit lost in the C code as the programs get bigger as it's not as wordy as basic and find myself constantly having to look up what things mean. I also find you can get a bit lost trying to constantly make the code more efficient so that it performs as you want on the chip. I guess that's just practice and the difference between someone who's good and bad at it though :)

How do you find the code on this in comparison?

Link to comment
Share on other sites

I have used PICs since the 90s for work and hobby. But there are many better things now (Arduino, Rasberry Pi) which are way more powerful and easier to use.
The Arduino code is easier to use, more powerful, more useful built in commands, many more libraries available, bigger community. Certainly with the RAM & program memory available on the Arduino its far less common to need to optimise.
Theres always many more than one way to do things, you can start with a messy program that works, but as things get more complex it helps to keep things tidy and follow good practice. e.g create functions rather than lots of inline code, comment your lines so that you can read it later, use constants and variables effectively.
But that comes with practice (or a software degree ;)), my hope with this thread is that people just play and learn. These devices are very powerful, and you can do so much more that you would think possible if you just give it a try. 

Link to comment
Share on other sites

On 22/09/2016 at 8:56 AM, HoSS said:

If you are starting out and want that speed/memory benefit of the new version cpu, i would go with the Zero board as it maintains sheild compatibility. https://www.adafruit.com/products/2843

The other thing to watch is some boards are 5 volt, and the shield's are 5v too, and some are 3.3v... the DUE (now retired I see) and the zero are 3.3v, so the shield's may not be quite compatible.


 The micro is a handy little 5v board, if you are getting into this, I would suggest buying a bit of breadboard, and either jumpers or strip some network cable for your prototyping, that will allow for quick setup of digital and analogue inputs and output simulations and then your code snippets can be reused (or a sheild with some breadboard on the top)

There are also even more interesting small microprocessors like the attiny85...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We use cookies to ensure you get the best experience. By using our website you agree to our Cookie Policy