Learn Programming and Electronics with Arduino

by Programming Electronics Academy

Video lessons on learning programming and electronics with Arduino. This is part of our Arduino Crash Course and Arduino Course for Absolute Beginners. It's designed to take someone with little or no experience in programming and electronics and get them fast-tracked to learning the skills to prototype using Arduino.

  

Latest Episodes

Arduino Course for Absolute Beginners 2nd Edition


Video Download

Posted on 29 August 2017 | 3:18 am


What is Arduino?

Have you seen some really cool stuff being made with a thing called Arduino? What is Arduino anyway?

It sounds like an Italian sandwich with extra cheese or something...

Well - it's that and a lot more. I hope this video can help explain some the basic premise of the Arduino!

 


Video Download

Posted on 28 April 2017 | 4:00 pm


How to change the font color in the Arduino IDE

Have you ever wanted to change the font color in the Arduino IDE?

Maybe it is hard for you to read the light grey comments in the Arduino IDE, or maybe you prefer something a little bolder.

Whatever your reason, in this short video, I demonstrate a relatively easy way to change the font color of comments.


Video Download

Posted on 27 April 2017 | 4:00 pm


Use Serial.print() to display Arduino output on your computer monitor: Part 2

In many cases while using an Arduino, you will want to see the data being generated by the Arduino. One common method of doing this is using the Serial.print() function from the Serial library to display information to your computer’s monitor.

In this week’s episode we will talk about the intricacies of the Serial.print() function.  This is the second part of a two part series on the Serial.print() function (Click here for the first part).

Here are the exact topics we will cover in this lesson:

  • Adjusting the number of digits to be displayed after the decimal point with the Serial.print() function.
  • Adjusting the format to display with the Serial.print() function
  • Formatting the output with text and tabs

If you enjoyed this video, you should consider signing up for our free Arduino Crash Course - it has 19 high quality video training classes to help build your imagination with Arduino.


Video Download

Posted on 26 April 2017 | 4:00 pm


Use Serial.print() to Display Arduino output on your computer monitor: Part 1

In many cases while using an Arduino, you will want to see the data being generated by the Arduino. One common method of doing this is using the Serial.print() function from the Serial library to display information to your computer’s monitor.

In this week’s episode, we will talk about the intricacies of the Serial.print() function.

This is the first part, of a two part series on the Serial.print() function. Here are the specific topics we will cover in this lesson:

  • Why would you want to use the Serial.print() function?
  • A brief overview of the Serial library
  • The basic use of the Serial.print() function

Like this video?  Sign up for our FREE Arduino Crash Course to get more videos that don't assume you have a PhD.

Why Would You Want to Use the Serial.print() Function?

You may know that a function is a programming tool - it performs a specific task for you. The Serial.print() function’s task is to send information from your Arduino to your computer, so you can see the value displayed on your computer’s monitor.

There are an endless number of reasons you may want to send information from the Arduino to a computer display, but two reasons really stand out to me:

The first reason is being able to see information that you are generating with your Arduino.

For example, if you have a temperature sensor hooked up to your Arduino and you want to see the value that the temperature sensor is recording, then you can use the Serial.print() function to send the data to a computer monitor via the USB cable. If you open up the serial monitor window (Tools > Serial Monitor), you will see the values streaming in from the Arduino.

Image of serial monitor widow using Serial.print() with Arduino to send bits one at a time

The other big reason to send information to a computer display using the Serial.print() function is for developing and debugging Arduino sketches.

Very often, when you are developing an Arduino sketch, what you end up coding does something differently than what you expected it to do. Maybe you have a variable that gets incremented every so often and blinks an LED when it reaches a threshold. When you upload the code to the Arduino, you notice that the LED is blinking more often than it should.

You can look at the code until your eyes bleed, but actually visualizing the variable being incremented [via the Serial.print() function], to see its values every time through the loop() can help explain what is happening very quickly.

A Brief Overview of the Serial Library

We can’t talk about the Serial.print() function, without briefly talking about the Serial library.

Generally speaking, a library is simply a collection of functions that all have something in common.

The print() function is part of a library called the Serial library. Now, it's not cereal like Cheerios or Captain Crunch we're talking about - it's serial as in “one after another”.

Serial.print() with Arduino send bits one at a time

The serial library allows us to interface the Arduino with other hardware, like a computer.

In order for us to use the functions of the Serial library, we have to initiate serial communication - to do this we use the Serial.begin() function. Serial.begin() needs to go in the setup().

 void setup() {    //Initiate Serial communication.   Serial.begin(9600);  } 

Now for reasons beyond the scope of this discussion, it is convenient to use the number 9600 in the Serial.begin() function. The value 9600 specifies the baud rate. The baud rate is the rate at which information will pass from the Arduino to the computer, or in the other direction.

The Basic Use of the Serial.print() Function

Let's talk about how to use the Serial.print() function.

Say we have a sketch. This sketch has a variable called coolFactor.

I want to be able to monitor the value of the coolFactor variable – that is, I want it displayed on my computer screen. A perfect use for the Serial.print() function!

The first thing we must do in the Arduino sketch is begin serial communications. Like we just said, we use the Serial.begin() function and place it within the setup() of the sketch.

 //A variable to hold the level of coolness int coolFactor = 3;  void setup() {    Serial.begin(9600);  }  void loop() {    //Send the value of coolFactor to the the Serial port.   //So we can see it in the serial monitor window   Serial.print(coolFactor);  } 

Now in the loop(), if I want to display coolFactor’s value with print(), I simply type Serial.print() and in the parenthesis I type the variable name.

If we upload this sketch to the Arduino, the value of coolFactor will be sent to the serial port every time through the loop(). In the Arduino IDE, if you open up the serial monitor window [Tools > Serial Monitor], you will see the values streaming down.

In next week’s episode, we’ll talk about some more intricacies of the Serial.print() function.

If you enjoyed this lesson, I welcome you to join 1000's of students who have enjoyed our free Arduino Crash Course - it's a 19 part video training series on using Arduino (You can sign up below).


Video Download

Posted on 25 April 2017 | 4:00 pm


How to make a secret knock detector to trigger anything with only an Arduino and a few cheap components

There are a couple good use-case scenarios for making a secret knock detector using an Arduino.

  • You are a spy who needs to authenticate your cohorts
  • You are a super hero who wants a secret knock to open the entrance to your lair

Whatever the reason - by the end of this tutorial you will know how to use an Arduino, a piezo transducer and a couple other cheap components to make secret knock detector.

Here is an overview of exactly what we will talk about in this lesson:

  1. The components you will need and how to set up this simple circuit.
  2. The concept of operation of this secret knock detector
  3. A thorough description of each block of code in the Arduino sketch
  4. Why North American grizzly bears love piezo transducers

For this secret knock detector circuit you need:

  • Arduino (I use the Arduino Uno) [1]
  • Solderless breadboard [1]
  • 1 Mohm Resistor [1]
  • Piezo transducer (aka buzzer) [1]
  • Jumper wires [4]
  • 5.1V Zener diode (for extra protection) [1]
  • No spill stopper for a “to-go” coffee cup

How to set up the Circuit:

This is a really simple circuit to setup, below are step-by-step instructions and a breadboard diagram.

  1. Place the piezo transducer on the breadboard, with the positive lead and the negative lead on separate rails.
  2. Connect the positive lead to pin A0 on the Arduino and the other lead to ground.
  3. Finally, use the 1Mohm resistor to connect the leads of the piezo transducer.
  4. As an additional level of protection, you might consider adding a 5.1V zener diode between the leads to protect against high voltage spikes from frying your input pin - you might call it a cheap insurance policy.

Piezo buzzer circuit with Arduino

An Overview of this Secret Knock Detectors operation

Here is the basic concept of how this will work.

We want something to happen when you tap out a secret code.

We will create a sequence of soft and hard taps - this will be our secret code which will be represented as 0’s and 1’s in an array.

For example:

 secretKnock[secretKnockLength] = {0, 0, 1, 0}; 

The code above represents a secret code of soft , soft , hard, soft .

The piezo transducer will turn the mechanical pressure created by the tap into a signal that the Arduino analog pin can read. The level of the signal will determine whether a tap gets characterized as soft or hard.

The threshold of a soft vs hard tap need to be determined empirically, once you have the circuit built - it will depend on what you have the piezo transducer attached to - I have mine taped to a piece of paper.

You should start with the default threshold values provided in the sketch and change them to suit your specific setup.

Once a tap signal is picked up by the Arduino, the sketch will compare the entered sequence of taps to the secret code, one tap at a time.

If the code is entered correctly, then we will trigger an action on the output pin. In this code below, we trigger an LED to turn on for a couple seconds - but you could trigger a servo arm, a pump, or whatever you might need.

If the code is entered incorrectly - nothing happens.

Here is the code for your hacking enjoyment:

 /* A simple sketch to detect a secret knock using a piezo transducer  Created JUL 2015 by Michael James http://www.programmingelectronics.com/  This code is in the public domain  */  const int outputPin = 6;    // led indicator connected to digital pin const int knockSensor = A0; // the piezo is connected to an analog pin const int thresholdHIGH = 150;  // threshold value to decide when the detected knock is hard (HIGH) const int thresholdLOW = 120;  // threshold value to decide when the detected knock is gentle (LOW)   const int secretKnockLength = 4; //How many knocks are in your secret knock  /* This is the secret knock sequence  * 0 represents a LOW or quiet knock  * 1 represents a HIGH or loud knock  * The sequence can be as long as you like, but longer codes increase the difficulty of matching */ const int secretKnock[secretKnockLength] = {0, 0, 1, 0};  int secretCounter = 0; //this tracks the correct knocks and allows you to move through the sequence int sensorReading = 0; // variable to store the value read from the sensor pin  void setup() {    //Set the output pin as an OUTPUT   pinMode(outputPin, OUTPUT);    //Begin Serial Communication.   Serial.begin(9600);  }  void loop() {    // read the piezo sensor and store the value in the variable sensorReading:   sensorReading = analogRead(knockSensor);    // First determine is knock if Hard (HIGH) or Gentle (LOW)    //Hard knock (HIGH) is detected   if (sensorReading >= thresholdHIGH) {      //Check to see if a Hard Knock matches the Secret Knock in the correct sequence.     if (secretKnock[secretCounter] == 1) {        //The Knock was correct, iterate the counter.       secretCounter++;       Serial.println("Correct");      } else {        //The Knock was incorrect, reset the counter       secretCounter = 0;       Serial.println("Fail - You are a spy!");      }//close if      //Allow some time to pass before sampling again to ensure a clear signal.     delay(100);      //Gentle knock (LOW) is detected   } else if (sensorReading >= thresholdLOW) {      //Check to see if a Gentle Knock matches the Secret Knock in the correct sequence.     if (secretKnock[secretCounter] == 0) {        //The Knock was correct, iterate the counter.       secretCounter++;       Serial.println("Correct");      } else {        //The Knock was incorrect, reset the counter.       secretCounter = 0;       Serial.println("Fail - You are a spy!");      }//close if      //Allow some time to pass before sampling again to ensure a clear signal.     delay(100);    }//close if else    //Check for successful entry of the code, by seeing if the entire array has been walked through.   if (secretCounter == (secretKnockLength) ) {      Serial.println("Welcome in fellow Illuminante!");      //if the sececret knock is correct, illuminate the LED for a couple seconds     digitalWrite(outputPin, HIGH);     delay(2000);     digitalWrite(outputPin, LOW);      //Reset the secret counter to 0.     secretCounter = 0;    }//close success check  }//close loop 

If you enjoyed this lesson, you should join our free Arduino Crash Course - it has 19 Video Training lessons all about using Arduino (you can sign up below).


Video Download

Posted on 24 April 2017 | 4:00 pm


Arduino Pseudo Random Non-Consecutive Number Generator

In this video we demonstrate how to create pseudo random numbers with Arduino - with a useful twist.

This lesson was inspired by the following viewer question:

How do I create Random Non-Consecutive numbers with Arduino?

P.S. These are the best tutorials that a complete idiot like you could ever make, thanks.

-Chris


Let's overview exactly what we will talk about in today's episode:

  1. Talk about pseudo random numbers.
  1. Identify the problem - using an Arduino sketch to demonstrate.
  1. Discuss how we might solve the problem.
  1. Write an Arduino sketch that solves the problem.
  1. Review what we talked about.

Before we answer the viewer’s question it is important to talk about what a pseudo random number is.

A purely random number in the mathematical sense can't be predicted. The microcontroller that the Arduino uses (and for that case, most computers in general) can't really create pure random numbers.

What they create instead are called pseudo random numbers. These are numbers that appear to be randomly generated, but if studied over time a predictable pattern emerges.

Pseudo Random Numbers with Arduino

The bottom line is that the random numbers we create with Arduino can be predicted.

Now there are clever ways to create pseudo random numbers that act like the real deal – you can learn about one method in our video tutorial talking all about random numbers – but for this discussion, let’s return to our viewers inquiry.

Identify the Viewer’s Problem - use an Arduino sketch to demonstrate.

Ok, so let's go back to the viewers question, he wants to generate random numbers, but he never wants the same number generated two times in a row.

Let's write an Arduino Sketch to make this clear.

 //This sketch outputs pseudo random integers.  //A variable to hold pseudo random integers. int randomInt = 0;  void setup() {    //Initiate serial communication.   Serial.begin(9600);  }//Close setup function  void loop() {    //Create a random number and assign it to the randomInt variable.   randomInt = random(0, 10);    //Send randomInt to the serial port for displaying on the serial monitor window.   Serial.print(randomInt);  }//Close loop function. 

In the first block of code a variable that will hold the pseudo random integers is declared and initialized.

 //A variable to hold pseudo random integers. int randomInt = 0; 

In the setup() function we begin serial communication in order to display the numbers we generate on a computer display.

 void setup() {    //Initiate serial communication.   Serial.begin(9600);  }//Close setup function 

In the loop() we create the random number with the Arduino random() function and assign the output to the variable we had just created. The random() function can take two arguments 1) the minimum value of the number we want generated 2) the maximum value we want generated.

   //Create a random number and assign it to the randomInt variable.   randomInt = random(0, 10); 

I will use 0 for the minimum, and 10 for the maximum.

Every time through the loop, a new random number will be assigned the randomInt variable.

Finally, the value of randomInt is sent over the serial port to be displayed in the serial monitor window.

   //Send randomInt to the serial port for displaying on the serial monitor window.   Serial.print(randomInt); 

If you upload this code and open the serial monitor you will see in some cases where the same number shows up two times in a row.

Pseudo Random Numbers with Arduino

This is the problem.  The viewer doesn't ever want the same number two times in a row. 

Discuss how we might solve the problem.

So let's talk about how we might solve this problem.  We know we need to generate a random number.

What if we create a variable to track the previous random number?

Then we could use a condition that says something like "If the previous random number is equal to the random number that was just generated, toss that number out the window, and create a different one.”

The final thing we would need to do is set the previous random number equal to the new random number, that way we keep updating our previous random number every time through the loop().

Let’s Implement our solution in an Arduino Sketch.

Copy and paste this code into your Arduino IDE.  All you need is an Arduino board attached to your computer to make it work.

 //This sketch outputs pseudo random non-consecutive integers.  //A variable to hold pseudo random non-consecutive integers. int randomInt = 0;  //A variable to hold the previously assigned pseudo random non-consecutive integers. int previousRandomInt = 0;  void setup() {    //Initiate serial communication.   Serial.begin(9600);  }//Close setup function  void loop() {    //Create a random number and assign it to the randomInt variable.   randomInt = random(0, 10);    /*Check if the random number is the same as the previous random number.   If it is, then reassign a new random number until it is different from the previously set one.*/   while (randomInt == previousRandomInt) {      //Create a random number and assign it to the randomInt variable.     randomInt = random(0, 10);      //When a consecutive random number has been identified, indicate it.     Serial.println();        }//close while statement    //Set previousRandomInt equal to the current randomInt.   previousRandomInt = randomInt;    //Send randomInt to the serial port for displaying on the serial monitor window.   Serial.print(randomInt);  }//Close loop function. 

If you upload this code to your Arduino and open the serial monitor window you will see the numbers scrolling across the serial monitor window, but now you will not witness any duplicates side-by-side.  You may notice some X’s intermittently between the numbers, this is where the Arduino sketch identified a duplicate random number and generated a replacement.

Frame 4

If you look through the code, you will see this is accomplished with a While Statement.

I hope you can find some application for this simple process of creating a pseudo random non-consecutive number with Arduino.  Let me know what you think the comments!


Video Download

Posted on 23 April 2017 | 4:00 pm


A Review of Make: Magazine

I don’t subscribe to many magazines, but I love my subscription to Make: magazine.

And even though I rarely have time to sit down and enjoy a magazine in peace and quiet (parents forget what peace and quiet means), I still manage to get through my Make: magazine cover to cover. I like it that much!

This video is a short review of why I enjoy Make: magazine, and why I think you would enjoy it too.

Here are some highlights:

1. Make: Magazine is published every 2 months.
2. Filled with detailed step-by-step projects
3. Has interesting editorial articles about technology
4. Covers a lot of hobby electronics topics (like Arduino)
5. Has sections dedicated to skill building

Total Honesty:

Nobody paid me to make this review of Make: magazine. I made this video because I personally enjoy the content and rigor and think you might like it too.

That being said - if you are interested in subscribing to Make: magazine I would love for you to use the link below if you so choose:

Click Here to Check Out Make: Magazine

The above is an affiliate link. At no additional cost to you, I get a kick back from subscriptions that originate from the link. This cash allows me to furnish my mansion in downtown Manhattan, make necessary repairs to my yachts, and fuel the Lamborghini for joy rides.

Do you subscribe to Make: magazine? I would love to hear your thoughts in the comments.


Video Download

Posted on 22 April 2017 | 4:00 pm


Understanding the Arduino Sketchbook: Opening and Saving Arduino Sketches

Understanding how the Arduino IDE sets up its file directory system can spare you some confusion when it comes to saving, organizing and opening your Arduino sketches (or sketches you have downloaded from the internet).

This week’s episode covers the following:

  1. The Arduino Sketchbook folder
  2. How to change the default location where your Arduino sketches get saved
  3. What happens when an Arduino file does not have an enclosing sketch folder of the same name
  4. Where and how multiple tabs get saved in an Arduino sketch
  5. Why the pancreas of a woodchuck is better suited than our own

Want to fast-track your Arduino skills?

Click here to join our 12-part HD Video Course.

 

You are probably familiar with a file directory system on a computer. It's usually represented as a hierarchy of folders and files.

An example would be your C Drive - inside the C drive, you may have a folder for Applications, Users, and Systems files. Inside the Users folder, you might have subfolders for Documents, Downloads, Music, etc.

All the files on the computer get organized in this fashion, and Arduino sketches are no exception.

The Arduino Sketchbook Folder and Changing the Default Save Location

The default location where Arduino sketches you write will be saved is called the Sketchbook.

The Sketchbook is simply a folder on your computer like any other. It acts as a handy repository for sketches and is also where add-on code libraries get saved.

You can see the sketches in the Sketchbook folder by going to File > Sketchbook.

The default name of the Sketchbook folder is “Arduino” and the default location of the Sketchbook folder is in the “My Documents” folder (or just “Documents” for Mac users).

If your Sketchbook does not appear to be in this default location, you can see exactly where it is by opening the Arduino IDE and going to Arduino > Preferences.

Arduino Preferences

The default file path for your sketches is listed at the top of Arduino Preferences window. Mine is:

/Users/michaelJames/Documents/Arduino

When I save a file in the Arduino IDE, this “Arduino” folder is the default location where the sketch will be saved, unless I choose to change that location by selecting a different folder on my computer's directory.

If you want to change this default location, you click the Browse button next to the file directory path and choose a different place. Pretty simple.

Sketch Folders

If you go into your file directory system and open up the Sketchbook folder (again, named “Arduino” by default), you may see a bunch of folders that you didn’t make.

This is because every Arduino file must be inside a folder that has the same name as the file (there are some exceptions to this that we'll talk about in a moment).

Let me say that one more time because it is really important to understand.

Every Arduino file must be inside a folder that has the same name as the file

When I write a new sketch and save it, the Arduino IDE automatically creates a new folder that has the exact same name as the sketch itself. The Arduino file (which has the extension .ino) is placed inside this enclosing folder, which is called a sketch folder.

Sketchbook

If you go into the Sketchbook folder and change the name of the enclosing folder, it will create some issues. The first issue is that when you go to File > Sketchbook, the sketch will no longer show up! If you want to open this sketch you need to go to the .ino file in your directory and open it from there.

If you open a .ino file that is not inside an enclosing sketch folder of the exact same name, then you will get a pop-up from the Arduino IDE that says:

“The file “sketch_name.ino” needs to be inside a sketch folder named “sketch_name”. Create this folder, move the file, and continue?”

Popup

If you choose Cancel, the sketch will not open. If you choose OK, then a folder gets created (it will have the same name as the sketch) and the .ino file is placed inside it.

This sketch folder will be created in whatever directory the .ino file was that you tried to open. For example, if you tried to open a .ino file that was in your My Downloads folder, then the enclosing sketch folder also will be created inside the My Downloads folder.

Saving Tabs in Arduino

The exception to the rule about the sketch folder having the same name as the .ino file is when you create multiple tabs in an Arduino sketch.

The additional tabs do NOT need to bear the same name as the enclosing sketch folder.

Tabs

Once you get a handle on some of these intricacies of the Arduino IDE file system, it can really help to clear things up.

Please take a moment to leave a comment if you have any insights or thoughts about this tutorial. I would love to hear them!


Video Download

Posted on 21 April 2017 | 4:00 pm