Arduino – Tips and tricks

Tips-n-tricks for the Arduino

I have put together some useful snippets of information from experiments and crawling the web for ideas and solutions to problems I have encountered on various projects.. enjoy!


Checking Memory usage

Including the following code above the void setup() method allows you to get back the free memory available. I usually use a Serial.println() command to dump it out so I can see whats happening memory wise as things are running.

The Code:

extern int __bss_end;
extern void *__brkval;

int memoryFree()
int freeValue;
if((int)__brkval == 0)
freeValue = ((int)&freeValue) – ((int)&__bss_end);
freeValue = ((int)&freeValue) – ((int)__brkval);
return freeValue;

Using It:

void setup()
Serial.print(“MEM (before): “);
Serial.print(memoryFree()); // print the free memory
Serial.println(” bytes”);

Four useful links for the nRF24L01+

The nRF24L01+ is a versatile RF transceiver operating in the ISM band, here are 4 useful links to sites with information from beginner  to advanced.


DF-Robot LCD Initialization

I found that the standard LCD initialization code does not work on my DF robot LCD. The correct constructor is:

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


Selective header file import based on IDE version

Simple MACRO to include correct header:

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#include "WProgram.h"

Basic Input de-bouncing

Simple digital input de-bounce code, locks the program into a loop during debounce so its useable in simple apps only. A better version would be to enter on a 1ms timer, check and inc counters and then recheck on next 1ms entry. 15-20ms of stable state would be considered a correct and stable switch state. Also, ditch the boolean return and make it void, use a getState() method to give you the current state (not raw state from the switch).

const int inputPin = 2;	  // the number of the input pin
const int debounceDelay = 20;  // milliseconds to wait until stable

// debounce returns the stable switch state
boolean debounce(int pin)
  boolean state;
  boolean previousState;

  previousState = digitalRead(pin);	    // store switch state
  for(int counter=0; counter < debounceDelay; counter++)
	delay(1);			// wait for 1 millisecond
	state = digitalRead(pin);  // read the pin
	if( state != previousState)
	   counter = 0; // reset the counter if the state changes
	   previousState = state;  // and save the current state
  return state;  

Input de-bounce using interupt

User submitted code to do it via an interrupt, could have more checks and a stable counter logic but the concept of using an ISR and entering on an interrupt should be expanded on.

 * ISR Debounce

// use 2ms debounce time
#define DEBOUNCE_TICKS (word)microsecondsToClockCycles(2000)

extern volatile unsigned long timer0_overflow_count;
word keytick;  // record time of keypress

void setup()

void KeyPress()

// returns true if key pressed

boolean KeyCheck()
    if (((word)timer0_overflow_count-keytick)>DEBOUNCE_TICKS)
      return true;
  return false;

void loop()                    
  if (KeyCheck())
    // do something useful




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s