Using Random Numbers with Arduino

This video tutorial talks about using the random() and randomSeed() functions with Arduino.  It is pretty straight forward but there are some intricacies worth noting.

Creating truly random numbers is harder than you might think.  The closest we can get in Arduino and just about anywhere else is using pseudo random numbers.  That is, numbers that mimic randomness, but in fact do have a pattern if analyzed for a long enough period.

Continue reading

3 Ways to Use Acceleration in an Arduino Sketch

This tutorial covers three easy ways to implement some form of acceleration in your Arduino sketches.  It will cover the following:

  1. Linear Acceleration
  2. Exponential Acceleration
  3. Messing Around with the Square Root function

What is Acceleration?

Acceleration is the rate of change of something over time.  Acceleration would help describe things like:

  • Have fast can a stopped car get to 88mph?
  • If I do a wing-suit jump from the top of Mount Everest, how fast will I be when I am gliding over base camp?
  • How can I gently slow down the movement of a mechanical servo before it comes to rest?

We can implement acceleration when programming Arduino quite easily with just a couple lines of code.

The basic concept is that a variable will need to change  -  how it changes over the course of the sketch is what we are interested in.  Below are three examples of how we can code change in variables so they display acceleration.

Linear acceleration

Linear acceleration is a constant rate of change.  It can be represented by a sloped line on an x-y axis.


A simple way to use it is to increment or decrement a variable every time through a loop like below…

You can see in the code above, that each time through the ‘for loop’, the accel variable is decreasing by 100.  If accel started at 1000, this is what accel would look like with respect to the number of iterations of the for loop.

Accel Chart_OP

You can see that the accel variable decreases at a set rate over time in a linear fashion.

Exponential Acceleration

Exponential acceleration changes over time too, but it does so more dramatically (It’s basically a drama queen).  At first the rate of change in the variable is small, but once it begins to increase it really takes off!


You can use the power function built into the Arduino IDE to make a variable change exponentially. The power function syntax is quite easy.

The parameter base is the number that get raised to the power of the exponent.  Like 2 to the 5th power.  Below is an example of how you might code it to change a variable exponentially over the course of a ‘for loop’.

Looking at a chart of accel over the course of the ‘for loop’, we can see that the way it changes is much different than linear acceleration:

Expo Chart_OP

The theme for exponential growth is that is starts out slow, but once it starts going it really get’s going!

Using the Square Root  Function for Acceleration

The square root function can also provide some flavor to the output from your Arduino.  It’s syntax is simple:

It returns the square root of the number or variable that you put in the parenthesis.  Below is an easy way to use it in a ‘for loop’:

Let’s look at how accel changes over the course of the for loop(let’s say it started at 10,000):

sqrt Chart_OP

You can see the a rapid deceleration in the value of the accel variable of the ‘for loop’.

Practice With Acceleration:

If you want to take a look at what these types of acceleration will do in “real” life, then set up the circuit below.

You Will Need:
  • (10) LEDs
  • (10) 220 Ohm Resistors
  • (1) Jumper Wire
  • Arduino Board
  • Bread Board
  • Black Marker
Set Up The Circuit:

Start by by placing one resistor at pin 2 on your Arduino.  Connect the other end of the resistor to the breadboard and then connect the long leg of the LED to the resistor.  Place the short leg of the LED in the outer ground rail of the bread board.



Then add 9 more resistors and LEDs across – you should stop at Pin 11 on the Arduino.  Then add a jumper wire from GND on the Arduino to the shared ground on the breadboard. (All LEDs not pictured).


That is pretty much it for the circuit.  Below are the code examples for the different ways to program acceleration that we talked about:

Here is the code for the linear acceleration:
Here is the code for the exponential acceleration:
Here is the code for messing around with the square root:

I hope the lesson was helpful.  The idea of adding acceleration to a sketch can seem a bit intimidating at first, but as you can see, it turns out to be a rather simple addition of code.

Try on Your Own Challenge:
  1. Can you make the fade amount of an LED accelerate/deccelerate?
  2. Can you add a potentiometer to the circuit above and find a way to control the amount of acceleration – either linear, exponential or otherwise.


How to Make an Arduino Water Detection Alarm

This tutorial will explain how to make a simple water detection alarm using an Arduino.

Here is the breadboard circuit for the project: Combined_Speaker_WaterDetector_bb_OP Here is the code that is used in the video when making the alarm portion:

DeBugging an Arduino Sketch #003 :: Cleaning up the Code

This code comes from a course student [Arthur] working on a challenge.  The challenge was to blink 3 LEDs in sequence back and forth and use two potentiometers to adjust the timing of the blink and the brightness of the LEDs.

Arthur did a fantastic job and his code works great – but there is always room for improvement, and he was seeking some pointers.  In this video I walk through the code and make some recommendations.

Want to get the most from this video?  Then copy the original code from below and see if you can determine how it works and how you might clean it up a little yourself.  Learning to read other peoples code is huge part of learning programming – give it a shot!


The three big themes I address in this code are:

  1. Variable naming conventions and comments
  2. Removing unnecessary code
  3. Creating a more responsive algorithm

Here is the original code:

Here is how to  build the circuit on a breadboard:

Made with Fritzing
Made with Fritzing



DeBugging an Arduino Sketch #002 :: Devil in the Syntax

This Debug video  comes from Juan, one of the students in the Arduino Course  for Absolute Beginners.  It focuses on some very common errors in Syntax.

The take aways are this:

  • Capitalization matters
  • Keywords should be orange-ish in color – if they are not, they are probably mistyped
  • Semi-colons belong at the end of every complete statement
  • Verify your code often!  It helps you snub out bugs as they are introduced

Continue reading