FREE USA SHIPPING ON ORDERS OVER $100 On Dr.Duino.com

Multithreading in a single Arduino, is it possible?

This morning I woke up to an email from a customer asking if multi-threading within a single Arduino is possible.

Meaning, can you do multiple things at the same exact time inside a single Arduino.

This is a really interesting question because at face value, one might think that this is possible.

After all, a single sketch could have a ton of functions or “things” it’s doing.

When you run the code, It might even appear that everything is actually happening at once BUT, this is just an illusion

A sleight of hand if you will. Let me explain.

For the purposes of this blog we will be talking about the two most common Arduino’s ever made.

The Arduino Uno (1) and Arduino Nano(2).

 

(Image credit arduino.cc)

They both use the same chip at its center the Atmega328P (called a microchip or MCU), one is just physically smaller than the other making it ideal for making projects where space is at a premium.

This particular one is an 8 bit up to 20 Mhz MCU have a look at the specs for it. 

 

(Datasheet source microchip.com)

Compared to what’s inside of your computer, these might seem slow, but for most Arduino projects, this is lightning fast.

AND… this is exactly where the sleight of hand comes in.

If you recall, all Arduino sketches are made up of two sections.

The Setup and the loop, where the setup is only run once at startup and the loop is what runs continuously for as long as power is applied.

Have a look at this piece of code below.

After the program completes running whatever is inside of the Setup(), it then enters the loop().

The Loop() in this code has 3 functions inside of it which will run sequentially forever.

So function1() runs first, then function2(), funciton3() and when its complete with funciton3(), it goes back to function1(). So on and so forth, forever!

The thing is, this is all happening at lighting speed and to the human eye, it might appear as though everything is happening all at once.

But the reality is that only one thing is ever happening at any one time!

Now… lets say that you needed to stop whatever the MCU was doing while it’s in the middle of executing something inside of function2().

You could absolutely do that using something called an ISR (interrupt service routine), and this might also give the appearance that more than one thing is happening at once.

But again, that’s just an illusion.

What the MCU is doing is what’s called “Servicing the interrupt” meaning, the code stops executing whatever its currently doing and goes and takes care of whatever code is inside of the ISR.

But at the end of the day, its only ever doing one thing at a time it’s just happening so fast, its imperceptible.

But what if you actually needed to do two things at the same exact time?

Well, for that you would need multiple Arduino's which act independently. Depending on what you are doing you might then have to notify the other one another via,say the serial port.

Or you could change your architecture entirely and use whats called an FPGA as those can do multiple things at once. But that's a completely different story for another time.

For me, I have yet to find a need to use more than one Arduino Uno or Nano in any of my designs.

But, that is ultimately up to what you are doing and how much horse power you need. I find, that I can do just about anything with a single Uno or Nano.

Well.. that's it, I hope that answers your question if an Arduino type MCU can do multiple things at once!

If you are looking to get started with Arduino OR are a seasoned pro and need a reliable and easy to use development platform, I'd like to invite you to come check out our entire line of Arduino Uno and Nano based shields.

If you have some electronics experience the Dr.Duino Explorer is the choice for you. Just click the photo below to find out more about.

If you're new to Arduino and electronics, then the Dr.Duino Pioneer version is the best choice. Click the photo below to learn more.

 

Which one will you choose, click here to learn more about the Explorer or here to learn about the Pioneer!