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: 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.

Click to open Arduino absolute value page.



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.

Click to open Arduino analogRead value page.



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:


Click to open Arduino analogWrite value page.


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.

Click to open Arduino String to int page.



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.

Click to open Arduino delay page.



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.

Click to open Arduino digitalWrite page.



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.

Click to open Arduino for-loop page.



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.

Click to open Arduino if-else page.



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.

Click to open Arduino int to string page.



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.

Click to open Arduino map page.



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.

Click to open Arduino millis page.



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.

Click to open Arduino pinMode page.



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

Click to open Arduino pulseIn page.



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.

Click to open Arduino shiftIn page.



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 implementaion of part of the SPI protocol and is consequently slower but can operate on any pin.

Click to open Arduino shiftOut page.



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.

Click to open Arduino switch-case page.



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].

Click to open Arduino string page.



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.

Click to open Arduino strtok page.



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 implemnent 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.

Click to open Arduino while-loop page.







×
Top of page

Arduino reference.
   abs
   analogRead
   analogWrite
   atoi
   delay
   digitalWrite
   for-loop
   if-else
   itoa
   map
   millis
   pinMode
   pulseIn
   shiftIn
   shiftOut
   switch-case
   string
   strtok
   while-loop

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