An Arduino Reference: Your Essential Guide to Arduino Functions - they don't always work the way you think they do! Easily improve your code by learning all about these functions and avoid common problems.

If you're finding the Arduino Reference a bit dry you need this page.

  • Do you need a practical reference to Arduino functions?

  • You found it!

  • Find out how you really use Arduino functions.

  • Some of them don't behave the way you think!

  • Practical examples and analysis of common Arduino functions.

On this Arduino reference page you can find links to common Arduino functions so you can find out how they work, and how you should use them. Some functions are obvious while others have obscure and subtle problems of which you should be aware e.g. abs, strtok, map and more.

Contents [show]

Arduino Reference: What Language does Arduino Use?

Find out the 'real' language that is used - It is probably not what you thought! What are the real differences when programming using the Arduino IDE compared to using a traditional compiler.

This is a more complicated question than just "what compiler the Arduino system" uses; it is more about the complete Arduino environment.

Arduino Reference: Arduino Hello World

Easily Understand How it Really Works! Find out How to Create the Arduino Hello World code, and Find out Exactly How and Why it works. It looks Simple but there's a lot going on Under The Hood!

Find out exactly why this code works - it is simple code and only requires an Aurdino Uno, a USB cable and a PC; but there is a lot of stuff going on to make it all work. Find out how it works here:

Arduino Reference: Arduino Print to Serial

What functions do you need to print serial data to the serial terminal in the Arduino IED?

Why is the USART not called an RS232 module?

How exactly does the serial link operate if its already in use for programming the Arduino? Can you use pins 0 & 1 for normal digital I/O?

Arduino Reference: Serial Begin

Find out exactly what 'Arduino Serial begin' does, and what it's for; Why you shouldn't use the number 9600...what does it mean anyway? Find out here.

Serial begin initialises the serial hardware module in the Arduino so you can communication from PC to Arduino or vice versa. The number 9600 is not the optimum number to use; Find out why in the link below:

Arduino Reference: Serial Available

Or is Serial data Ready Yet? Normally your program will work the way you expect but sometimes it won't. What are the interactions that could make your program Fail? Find out Here.

Serial available can cause problems when your Arduino is busy doing lots of other work. Find out what the effects are and what to do about it.


Arduino Reference: Serial Read

Arduino Serial Read: Or how to control your Arduino from a PC!  Find out how the Serial read function works to receive multiple bytes using interrupts and buffers. Understand its Internal Operation so you Don't Fall into a Trap.

Arduino Serial read looks like a simple function and in normal use it behaves exactly as you would expect making reading data from the serial port a trivial and easy matter. However it is useful to understand its internal operation as you can sometimes fall into a trap where you lose data. Find out more in the link below...

Arduino Reference: abs

Arduino absolute value: has One Problem. It is simple enough and trivial to understand! So why is it not working correctly?

The Arduino absolute value function is a fairly simple function but there is a trip-up in the way it is implemented on the Arduino system. This is likely different to other C implementations.

Arduino Reference: analogRead

AnalogRead is the function that reads the value from the internal ADC and If you want to easily find out about your Arduino ADC then you need this page. Find out how it works and how to use it for best results.

The function analogRead reads from one of six analog input pins giving you a value from 0 to 1023 that represents an analog voltage from 0 to 4.9951V. To find out why you can never read a value that represents 5V click the link below.

Arduino Reference: analogWrite

AnalogWrite Impossible Analog; using Digital Pins. How to generate an Analog Output varying from 0V to 5V using only Digital pins! It sounds impossible as digital pins output only 5V or 0V.

The secret to getting a continuously varying analogue output from a digital signal is twofold:

  1. PWM output (Pulse Width Modulation) and
  2. Averaging.

Find out the capabilities of the internal PWM generators in the Arduino:

Arduino Reference: atoi

Arduino String to int: How to convert a string into an integer. Find out the standard function for this operation and learn how to code it yourself.

You can find code and the algorithm used to perform this useful task. Another useful algorithm on the page is a radix conversion variation of atoi that allows you to convert from hexadecimal or binary strings.

Arduino Reference: delay

Why you need it and How to Use it. This is an Extremely Useful function which you can use to get small delays. However, sometimes it's not the right function to use – there is another!

The Arduino delay function takes an argument representing the number of milliseconds to delay. Although it is used a lot, it is a blocking function. When you get more complex projects you can not afford for the processor to do nothing (as occurs with the delay function). The alternative is the millis Arduino function.

Arduino Reference: digitalWrite

DigitalWrite is the function that lets you control output from Arduino pins. You can use it to control ICs,  LEDs or relays etc. very easily.

Here you can find out exactly how the function works with source code explanation and how to use macros to get faster operation.

Arduino Reference: float to string

Arduino Float to String: Is there a simple function that does this for you? What is the one thing you must do to ensure your program is reliable?: Find out here.

There is one function that allows you to convert a float into a string easily but it can cause problems.

Arduino Reference: for-loop

How you can use it the Right Way. Easily repeat blocks of code saving processor memory and simplifying access to array data. Avoid off by one errors and how to create an infinite loop.

The for-loop is the main construct used to step through a series of values. It allows you to :

  • Set the start value of the variable,
  • Select the increment of the variable at each step,
  • Determine when to exit the for loop.
  • Execute a block of code n-times.

So you can set the start and exit conditions while updating a variable up or down. In addition you can repeat a block of code while the variable is updated.

Arduino Reference: if-else

Understanding Arduino 'if-else' for your programs. How to use the Arduino 'if-else' statement, and use its different forms in your code. Learn about the ternary operator (?) for ultra-compact conditional operation.

The Arduino conditional construct allows your code to make decisions based on input variables. It gives you the standard if, then, else decision making code that is common to all programming languages.

Find out how it is used in C/C++ in the link below.

Arduino Reference: itoa

Arduino int to string: How to convert an integer to a string. Find out the standard form and Learn how to code it yourself. Also find the one subtle error you missed.

Here you will find out how to convert an integer into a string, using the standard function itoa but if you want to know how it really does its magic this page will also show you fundamental code for conversion of Arduino int to strings.

Arduino Reference: map

How to map a value from one range to another. It is a useful function but it may not do what you expect! Find out here why it may go wrong and how to use it correctly.

The Arduino map function should be an obviously simple implementation of mapping one range of values to another range of values but there is a subtle problem that will not evenly distribute the ranges. Find out how to fix it in the link.


Arduino Reference: millis

How to use Arduino millis to make Delays, One-Shots and simple Schedulers plus analysis of millis code; Plus - Find out why it Lies (a bit)!

The Arduino millis function, returns only the number of elapsed milliseconds since reset of the board. Unlike delay, millis is a non-blocking function. You can use it to time events while also allowing the processor to do other tasks. You can also uses it in an equivalent, but non-blocking way, to the delay function by calculating timing delays.

There is a subtle 'feature' in the millis code that forces it to occasionally skip values - find out more in the link.

Arduino Reference: pinMode

How can it be possible for an Arduino pin to be either an input or an output? Is INPUT_PULLUP just for button push detection? And did you forget Mr. Schmitt?


The function pinMode seems to be a simple function but there is a lot going on under the hood. Find out exactly how it works here.

Arduino Reference: pulseIn

Did you know that there are two versions of PulseIn? Which one you should use for best accuracy? Why are there two functions? Why must interrupts be on for one and off for the other?

You can use either to measure the high or low time of an input pulse. Find out how to use them in the link below.

The functions are:

  • pulseIn
  • pulseInLong

Arduino Reference: servo

For the Arduino servo library,Why is the output not between 1000us and 2000us? - and how to fix it. Find out what to download to make the library work and how to get super accurate Servo positioning.

There are two ways to initialise this Class  - find out the right way.

There are two position setting functions - find out which is more accurate.

Arduino Reference: servo with no library

You may be in the situation that you've used Timer 1 and 2 for something else but you still need to drive some servos.

The following link shows you exactly how to do it using only built in function of the Arduino code - no hardware timers - simply a software solution that works.

Arduino Reference: servo smoothing

Get rid of that jerky movement by smoothing the operation of your servo.

The question is: What profile should you use and what library is needed?

Arduino Reference: shiftIn

How to read data from a Serial Output Chip. It receives serial data from parallel to serial converter chips, Saving You Microcontroller Pins. Find out how it works and how fast it operates.

The function shiftIn is a serial to parallel converter that you can use to get data from chips that send out data when driven by a clock signal.

This is a software implementation of part of the SPI protocol and is consequently slower but can operate on any pin.

Arduino Reference: shiftOut

Control chips with a 3 wire Serial Interface. Use it to easily control many different chips with a serial interface. Find out how to use it, how it works and how fast it operates.

The function shiftOut is a parallel to serial converter that you can use to send data to chips that accept data when driven by a clock signal.

This is a software implementation of part of the SPI protocol and is consequently slower but can operate on any pin.

Arduino Reference: struct

Arduino Struct syntax allows you to contain related data in a single variable. Find out Exactly how to use them here with full Explanation and Code Examples.

Arduino Reference: switch-case

Avoid Problems and Learn the Right Way to use it. Find out how to write compact code with multiple conditions and how to reduce long lines of 'if-else' into short code. Get it right First Time.

The switch-case construct allows you to minify your decision making code if you use it in the right way. there is one pitfall that you should avoid: described in the link below.

Arduino Reference: string

C++ style string vs old style c string Compared. Should you use the Arduino String class or old style c strings? Is there a memory saving? Which is easier? Find out here with a Full Serial command decoder.

Find out the difference between C strings and C++. You can observe the differences with an example that shows how to decode a serial command stream in both C and C++ string versions.

Also find out if there are any problems in either C strings or C++ strings [to do with memory usage - hint C++ uses the heap].

Arduino Reference: strtok

How to Easily Extract Tokens from a string. Learn how Arduino strtok works - with walk-through examples - and how to use it to find multiple tokens. Find out why you can't change delimiters halfway through and avoid the one problem you'll probably fall for!

The Arduino strtok function is a function that steps through a string rejecting some characters (the delimiters) while keeping other characters (the tokens). At each call the function returns a pointer to each set of tokens in turn.

Arduino Reference: switch-case

Avoid Problems and Learn the Right Way to use it. Find out how to write compact code with multiple conditions and how to reduce long lines of 'if-else' into short code. Get it right First Time.

The switch-case construct allows you to minify your decision making code if you use it in the right way. there is one pitfall that you should avoid: described in the link below.

Arduino Reference: Variable Types

Learn Arduino Variable types declaration, memory usage and number ranges. Keep your program from Failing by Knowing about Number Ranges...


You can get some very subltle erros when you move code from one processor to another - find out why here:

Arduino Reference: while-loop

How you can use this loop in Two Ways. There are two forms of this loop construct which make it easier than using the for-loop. Also how to easily create an infinite while loop.

The Arduino while-loop is another construct that lets you repeat code. However in this case it is simpler that the for-loop. Since it only has a condition for exit it allows you to write clearer code if it can be used instead of a for-loop.

Unlike the for-loop you have to implement the variable increment or decrement within the while loop block. Sometimes you need this for the problem you are trying to solve.

The other form of the while-loop is the do-while-loop that always executes some code before testing the exit condition. This can also be useful in many use-cases.






Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



Privacy Policy | Contact | About Me

Site Map | Terms of Use