Tutorial, NodeMCU Ab Kurk Tutorial, NodeMCU Ab Kurk

Tutorial: Intro to the NodeMCU

This tutorial is a simple guide to get started with the NodeMCU one of the most popular forms of the ESP8266 on the market right now. The NodeMCU is a powerful board that is good on the pocket book, but has its drawbacks. We are going to explore how to set this board up with the Arduino IDE, and some other simple tips and trick to get you started.

Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction
New-Wireless-Module-CH340-NodeMcu-V3-Lua-WIFI-Internet-of-Things-Development-Board-Based-ESP8266.jpg

This tutorial is a simple guide to get started with the NodeMCU one of the most popular forms of the ESP8266 on the market right now. The NodeMCU is a powerful board that is good on the pocket book, but has its drawbacks. We are going to explore how to set this board up with the Arduino IDE, and some other simple tips and trick to get you started.

 

Material Needed For This Tutorial

Driver Installation

One of the drawbacks of this board is that you have to install a usb drivers to communicate with this board. You can download the driver from here. Before you try to install the driver be sure you have Administrator privileges on your computer.

After you have downloaded the driver installation package, and you have Administrator privileges you can start the installation. Use a Micro USB Type-B cable that is rated for data transfer.  Follow these steps to install your Driver

install.PNG
  1. Download Driver
  2. Connect your NodeMCU to your computer using the USB cable.
  3. Run the driver installation package  you downloaded as Administrator
  4. Click the install button

 

The driver should install without any problems. The main issue that people have is either not connecting the NodeMCU to the computer before starting the install, or not having the right cable, or not having the correct privileges on your computer. If your driver install fails first make sure all the above mentioned requirements have been met. If it still doesn’t work you have entered the realm of unsupported freeware, and sadly are on a journey of exploration that can be painful. I have not seen it fail when all the requirements have been met.

 

Preparing the Arduino IDE

If you have never worked with an ESP8266 type of board you need to install the drivers and libraries for this in your Arduino IDE. . The people at Sparkfun have a nice tutorial for this. You go there by clicking on this link: https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/installing-the-esp8266-arduino-addon

The NodeMCU form factor

IMG_5031.JPG

You might have noticed by now that the NodeMCU is a bit large. It won’t fit on a regular size breadboard (well it does but you have no room to connect anything to it). What I have done to make it fit is use a dremel tool and cut a normal breadboard in two along the spine (See picture) Now it will fit perfectly.

 

 

 

Pinouts

This is one of the greatest drawbacks of the NodeMCU for beginner users. The labels on the NodeMCU don’t correspond to the digital pins (GPIO pins) we know and love when working with Arduino compatibles. You need image ? to make sense of it.

For example pin D1 on the board corresponds with GPIO 5 (Digital pin 5 on your Arduino). The best thing to do is keep that image handy as a guide to find out what pin does what, and how they correspond to the Arduino world.

Specs

The board runs at 3.3v, and all pins can have a max load of 12mA and a 3.3v logic. When buying breakout boards or other components make sure they run on those specs. It also means you have to be careful when using the pins from the NodeMCU to power components. 12mA is not a lot and this makes it easy for you to overload a pin and fry your board. The board also has only one analog port A0, and it is for input only (like all other ESP8266).

You can power the board with an external power supply no greater than 12V. The documentation is spars, but what I have found is that it can handle 20V but the volt regulator will get dangerously hot. This is why I recommend having a power supply that supplies no more than 12V.

Onboard LED

Often we use an onboard LED in the development phase to error check. This board does not have the LED connected to pin/gpio13, instead it has it connected on pin/gpio 2.

Uploading a Sketch

Next we are going to upload the blink sketch to the NodeMCU. If you have followed the above steps you should have installed the hardware driver (driver name) and the Arduino IDE drivers and libraries we are ready to upload a blink sketch.

Configuration for the NodeMCU

Configuration for the NodeMCU

We start by selecting the NodeMCU 1.0 (ESP-12 Module) under the board options in the Tools menu (See Image for the complete configuration). See Image ? . The only thing that could be different on your computer is the COM port. You just need to select the COM port available to you under the Tools menu Port option.

Below you find the sample sketch to make the onboard LED flash on the NodeMCU

int LED_PIN= 2;

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_PIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_PIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

In Closing

Check back in about 14 days when I build a more elaborate project with the NodeMCU to see what you can do with this board. But for now if you enjoyed this tutorial please subscribe to my newsletter by filling out the form below, or go to my Facebook page by clicking on the link and follow and like the page. By doing his you will get an alert every time I post a new article. If you have questions or suggestions please email at akurk@thearduinomakerman.info me or leave it in the comments below. Have a great day and see you next time. 

 

Subscribe to our mailing list

* indicates required
Email Format
Read More
Helpful Tips, Arduino Ab Kurk Helpful Tips, Arduino Ab Kurk

Tips: How to ask for help

There is no shame in asking for help when you get stuck on your Arduino projects, but there are definitely right and wrong ways to ask for help. Often people are not sure what information needs to be displayed. Here are some simple tips on how to ask a question, and how to trouble shoot to be able to get the information needed for a question

Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction
Question-Marks.jpg

As you can imagine I often get emails with questions and cries for help. I actually enjoy these emails and try to help as much as I can. One of the issues I see is that some makers don’t know how to formulate a question to get the help they need. Just the other day I got an email from somebody who asked about a project I posted on Instructables a long time ago.

The project is for an oxygen analyzer to analyze dive tanks. The question was “ I get this weird error when I put sensor of make x on your sketch, what is wrong”? The problem with this question is it does not contain any information to find out what is happening. People are not able to help you if you don’t provide the correct information. If you ask this in a public forum people are not going to respond to your question.

Before you ask your question you have to do your due diligence. Below are some steps you have to take first before you ask for help.

What information do you need to supply?

You are asking for help, and may I add at no cost, you need to make it as easy as possible for people to help you. For example the person asking me about the oxygen analyzer should minimally have supplied what the error was and what changes he had made to the sketch.

If it is a question about a sample sketch that returns an error, you minimally need to supply what OS you are compiling on, the version of the Arduino IDE you use, and what libraries you are using, and of course the error message in as much detail as possible.  If you made alterations to the supply code give an explanation on what changes were made.

Minimal List of information you should supply

  • Sample sketch
  • Libraries used
  • Arduino IDE Version
  • What hardware used (Arduino related)
  • OS version you compiling on (If it is a compiler error)

Before asking for help first find out what is wrong

Before asking for help you need to try to figure out for yourself what the issue is. By doing this you also have a better understanding of the problem. It will also help you to understand the answers you are going to receive. Here are some tips on doing your own trouble shooting before asking for help.

Trouble with your hardware

First make sure your hardware is working. You do this by testing each component separately. If you create a temperature/humidity data logger that uses a sd card breakout board, make sure that each component functions alone with your Arduino. First make a sample sketch with the temperature/ humidity sensor and see if you get the data you are expecting, and do the same with the sd card breakout.

I do this for all my projects. I first test each sub component separately before combining them. This way I know I wired everything correctly and know that is not the issue. If everything seems to work separately, and it stops working when combined all the components you know there is a conflict. Find out what components are conflicting by testing them with only an Arduino and two components at a time.

Note:In the beginning try to use components and breakout boards from the big guys like; Adafruit, Sparkfun, Pololu, Seeedstudio. They always have tutorials on how to use their components, and forums you can ask questions in.

Now you have done your homework, and are ready to ask people for help. First create a diagram with something like fritzing to give a visual representation of how the components are connected. Supply the sample sketch you made to work with these components, and give as much info about the versions, makes, and models of your hardware. By doing this you will get better quality of help, and avoid the trolls making nasty comments when you ask for help.

Note: A sample sketch is a sketch that only contains the code needed to show the problem. It is a fully working sketch, but paired down just to reveal what the problem is. It is a good way to trouble shoot, and to provide enough code to people to see what the problem is

Use your serial monitor to debug

The programmers among us know a bit about debugging and how important it is. Normally you can add breakpoints (a spot the program halts so you can look at the values of variables) to see what is happening, but because this is not possible with an Arduino we use the Serial.println() function to debug your sketch. This is often done because your project is not responding as you thought it would be. When I write a sketch it is always full of these Serial.println() statements to see what is happening. I normally remove them from the final sketch as it looks cluttery (if that’s a word).

For instance you press a button and your Arduino is not responding to it’s request. By simply seeing if your digital pin is high or low you can see if the button press has an effect, or maybe your if() statement is looking for the wrong answer. A simple Serial.println(digitalRead()) will let you know if the digital pin in question is HIGH or LOW. Look at the following example where I put lines in for debugging:

  buttonState = digitalRead(buttonPin);
  //Next lines are fo debugging
  Serial.print("buttonState: ");
  Serial.println(buttonState);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }

This is often done in situations where complex calculations are in play using many different variable types e.g doubles, floats, and integers. By seeing what the actual value of these variables are before going into the calculation you can often find the problem.

Language Reference

Another common issue is when you are not sure what function to use or how to convert a data type. Before reaching out to someone, try going to the Arduino Language Reference page. This is an amazing resource with tons of information. This is my go to place when I am not sure what function to use, or what is the correct syntax.

Look here first and get an idea of what you want to use in your sketch and how to use it. Sometimes the information in this area is a bit thin and their examples could be better. If you get stuck and not sure how to get unstuck, that’s when you reach out to the community. First try to come up with an example sketch how you think it needs to go, and then post your question on your favourite place.

Sometimes just except the answer even if you don’t completely understand it

Sometimes the answer will be in the form of a cryptic piece of code that works for you but doesn’t make sense to you. Just run with it and try to figure out what part you don’t understand and google it. Most of the time (and it happens to me to) it is currently out of your scope of knowledge and will take a lot of reading, learning, and gaining more experience to understand it. As long as you know what part you don’t understand how to communicate with that code you can implement it in your code as a form of a black box (a black box is something you provide data to,  it gives you the answer, but you have no clue how).

In Closing

If you implement all of my recommendations, 80% of the time you will find your own answers. The 20% where you will need help,  you will look like somebody that wants help and shows that you have worked for it. It also gives the person answering your question an understanding of your experience level. Just saying “I am a noob” does not help when asking your question. If the correct information is not provided, people can’t help you.

Don’t be afraid to ask for help, but in the end you get out of it what you put into it. Don’t be embarrassed if you are not sure if what you did was right, if you show you want to learn there are many people out there who want to help you. Finally be proud of your accomplishments, and stand behind your work, even if you are not sure you are correct.  We all started off as a noob and had to learn and gain experience by asking the community for help. 

If you like this project and would like to see more of the same type, please subscribe to my newsletter using the form below or like  and follow my Facebook page. This way you get notified when a new post is available.  If you have questions or suggestions please email at akurk@thearduinomakerman.info me or leave it in the comments below. Have a great day and see you next time.

Subscribe to our mailing list

* indicates required
Email Format
Read More
Arduino, Tutorial Ab Kurk Arduino, Tutorial Ab Kurk

Tutorial:A guide to putting your Arduino to sleep

Tutorial:A guide to putting your Arduino to sleep

If you need to run your Arduino of a battery pack, you need to find a way to reduce it's power consumption. One of the the best ways to do this is putting your Arduino to sleep when it is not performing any tasks. This tutorial is a great place to start on learning how to put your Arduino to sleep.

Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction
sleeping arduino.png

Sometimes we are in a situation that requires us to put an Arduino in a place where plugging it in to the power grid is not an option. This happens often when we try to log information in a remote site, or only need to have your Arduino active at a specific interval/action.

In these cases putting your Arduino to sleep is the perfect thing to do.  Their attention is only required for a short amount of time e.g. log data in a specific interval, or put out an alert when a predetermined event happens. In this tutorial we are going to experiment with putting your Arduino to sleep and see how to turn your Arduino back on. 

This tutorial familiarizes you with the concept and has a small exercise to see what it takes to put an Arduino to sleep. In the next couple of blog posts (in 2 weeks or so)  I will show post a couple a projects that will show you how to wake your Arduino using a sensor, or a Real Time Clock module (RTC).

MATERIALS NEEDED IN THIS TUTORIAL

What board to use?

In this tutorial we will be using the Arduino Uno just because it is an easier board to prototype on. In a real live project I would use an Arduino Pro Mini for this. The Arduino Uno and the Arduino Pro Mini have very similar characteristics, the Arduino pro mini has a lot less hardware to power (e.g. the USB portion, extra leds, and some other stuff) thus using a lot less power. This is the reason why the Arduino Pro mini is a better choice.

To give an example a Uno uses between 30-40 mA when awake and about 19 mA when asleep. The Pro Mini uses 25mA when awake and 0.57 mA when asleep. As every mA matters when hooking it up to a battery you can see that there is no contest and the Arduino Pro Mini is the winner.

Note: As a beginner Maker the Arduino Pro Mini might be a bit intimidating, but there is no reason for it. Yes you need to solder the headers onto the board, and you need a FTDI cable to upload your sketch, but other than that there are no major differences.  

Sleep mode

When you look at the documentation of the ATmega328p (click this link for a copy of this document) processor used for both Arduino Uno and the Arduino Pro mini you notice there are many different sleep modes available. But in a real world scenario there is really only one mode that is useful; The Power down mode (SLEEP_MODE_PWR_DOWN).

When you put your Arduino to sleep it turns off all unnecessary components, reducing the power consumption of the MCU (Microcontroller Unit).  In this mode the only way you can wake it up is the use of an external influence (e.g. we give it a nudge to wake up). We will examine how to do this a bit later in this tutorial.

Interrupts

Before we go into the code to put an Arduino to sleep we need to understand the interrupt concept. The best way to describe it is ; You are working on something you really need to concentrate on. You wear headphones blasting your music loud to drown out your surrowndings . You are so concentrated on this that the outside world is lost to you. The only way to get your attention is by giving you a nudge. After you receive this nudge you pay attention to what the interruption is about, and after dealing with it you put the music back on and continue with your task.

Note: I am not going to go to deep into what interrupts are good for, but if you want to learn more about this concept check out my tutorial (Using Interrupts to improve the functionality of your project) on this topic

Most true Arduino’s have a couple of pins that do just that. The Uno and the Pro Mini have 2 pins (d2 and d3) that have the capability to interrupt what the Arduino is doing. With this we can nudge the Arduino back to a waking state.

Putting your Arduino To Sleep

You can download the code for this section for here.

Let’s look at the code for this section

Image_1 click to enlarge

Image 1 contains the code snippet that loads the library that contains everything we need to put your Arduino to sleep, We also declare the variable interruptPin for digital pin 2. We will later use this for making pin 2 an input pin. Next we will look at the Setup() function.  

Image_2 Click To Enlarge

Image_2 Click To Enlarge

Image 2 has the code for the Setup() function. It is all straight forward. We declare digital pin 13 as an output pin (LED_BUILTIN is a build in variable for digital pin 13 where an onboard led is connected to).  We are using the LED as an indecator for when the Arduino is asleep (when LED is on Arduino is awake, when off the Arduino is asleep).

On line 18 we set digital pin 2 as an input pin. You notice we use INPUT_PULLUP instead of INPUT. By doing this we use the build-in pull-up resistor to prevent the pin from flopping between HIGH and LOW when nothing is attached to it (same thing you would do with a button).

Next we are going to the main loop() function;

Image_3

Image_3

On line 23 we put in a delay of 5 seconds before we call the Going_To_Sleep() function on line 24. This is just so you can see that the onboard LED is on to show your Arduino is awake, and the moment we call this the Going_To_Sleep() function the LED goes off to indecate the Arduino is asleep. 

Next lets look at the Going_To_Sleep() function itself; 

Image_4 Click To Enlagre 

On line 28 we call the sleep_enable() function which is part of the avr/sleep.h library. It enables us to put the Arduino to sleep, without calling it we can't put the Arduino to sleep. On line 29 we attach an interrupt to pin 2 as I explained in the Interrupt section.

Syntax

attachInterrupt (interrupt, ISR, mode);

As you notice we use a 0 to indicate that we are using pin 2. This is because the Arduino Uno/Pro Mini have 2 interrupts. Interrupt 0 is connected to digital pin 2, and Interrupt 1 is connected to digital pin 2.  The ISR is the function name that is called when the interrupt is called. In our case it is called wakeUp.  The mode is what needs to happen to the digital pin to call the interrupt. In our case the pin needs to be pulled LOW (to GND).

On line 30 we set the set of sleep mode we want. In our case it goes and shuts down everything it can. On line 31 we turn off the LED, and on line 32 we wait a second to give the board the time to turn the led off. Next we actually put the Arduino to sleep with the sleep_cpu() function.

The code halts here until the interrupt is called. After waking up the Arduino will first execute the code in the wakeUp() function and then will continue with line 34 printing the wakeup message on the serial monitor, and on line 35 turning the LED back on.

Image_5

In the wakeUp() function we print a line to the serial monitor to let you now the interrupt has been called. The next two lines are very important to make sure we do not get an unintentional loop where the sketch can get stuck, and making your project fail. 

On line 40 we disable the sleep function, and on line 41 we detach the interrupt from pin 2. As I said we do this to prevent a possible endless loop situation.

Exercise 1

Step 1)

Now it is time to upload the sketch. But before doing that put a jumper wire in d2. Just leave it unplugged on the other end for now. Load your sketch and wait 5 seconds for the LED to turn off and the Arduino to go to sleep.

Step 2) 

After the LED turns off insert the other end of the jumper wire in a GND pin on your Arduino Uno. This will pull pin 2 LOW triggering the interrupt, thus awaking the sleeping Arduino. After the LED comes back on you can remove the jumper wire out of GND and 5 seconds later the Arduino goes back to sleep.

 

 

IN CLOSING

Now you know the principles of what it takes to put your Arduino to sleep. As you see it is a very simple process. Finding a good mechanism to control this project is sometimes a bid more problematic. For this reason I will post a couple example projects on how to wake up an Arduino using a sencor (e.g. motion sensor), and a Real Time Clock module (RTC) that will be designed so you can use them and integrate that into your own project.  

If you like this tutorial and would like to see more of the same type, please subscribe to my newsletter using the form below or like my Facebook page. This way you get notified when a new post is available.  If you have questions or suggestions please email me or leave it in the comments below. Have a great day and see you next time.

Subscribe to our mailing list

* indicates required
Email Format
Read More
Tutorial, Arduino Ab Kurk Tutorial, Arduino Ab Kurk

Halloween Hack: How to automatically activate your Halloween Arduino Project

Halloween Hack: How to automatically activate your Halloween Arduino Project

With Halloween coming around the corner it is time to see how we can automate some scary projects. We are going to explore two of my favourite ways to do this. First we are going to look how to integrate a pressure plate switch (a big push button you step on which we are going to build), then we will look at how to connect a motion sensor (Passive Infrared Sensor or PIR) to your scaretastick project for the maximum scare factor. I recommend you also watch the video below to get the most out of this tutorial.

Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction

With Halloween coming around the corner it is time to see how we can automate some scary projects. We are going to explore two of my favourite ways to do this. First we are going to look how to integrate a pressure plate switch (a big push button you step on which we are going to build), then we will look at how to connect a motion sensor (Passive Infrared Sensor or PIR) to your scaretastick project for the maximum scare factor. I recommend you also watch the video below to get the most out of this tutorial.

Integrating your Pressure Plate switch

One of the most used activators of any Halloween projects is a pressure plate switch, which you can hide under a mat. Your unsuspecting victim steps on your door mat activating your amazing Halloween project, and scares the socks off them.

But how do we connect this to your Arduino and write the code for it. First let’s look how we can build a simple pressure plate switch:

Materials and tools needed for your pressure switch:

Materials:

  • 3 identical pieces of corrugated cardboard about half a centimeter thick
  • Aluminum foil
  • Masking tape
  • Glue or double sided tape
  • Hookup wire long enough for your purpose

Tools:

  • Utility Knife
  • Ruler
  • Wire stripper

Building Steps

Before you begin make sure the width of your cardboard is the same or smaller than your aluminum foil you are going to use.

Step 1:

With your utility knife cut the center out of one of your pieces of cardboard leaving a border of about 3 cm to 4 cm.

Step 2:

 

Use glue or double sided tape to affix your aluminum foil to the 2 remaining pieces of cardboard. Make sure that your aluminum foil is well adhered to the cardboard so it doesn't sag.

Leave a small border (4cm / 1 inch) without aluminum foil around the edges (In my picture it is not visible because I forgot to do it). If you don’t do this you have the chance that you create a short, resulting in your project not functioning correctly (which happened to me ).

Step 3:

IMG_4467.JPG

Strip one end of your hookup wires exposing 5 cm/2 inches of the copper wire inside. Lay the 2 pieces of cardboard next to each other with the aluminum foil facing up. Use masking tape to tape the exposed copper wire to the aluminum foil.

Step 4:

Affix the 3rd piece of cardboard (The one with the center cut out) to one of the aluminum foil covered sides. Now place the other Aluminum foil covered piece of cardboard on top (aluminum foil facing down) creating a sandwich. If you have a multi meter test the switch(before taping this all together) to make sure you don't have a shorts.

Tape the 3 layers of cardboard together so they are fixed in place and can’t slide or move, and your switch is completed

 

Connecting the Pressure Plate Switch to your Arduino

Now you have created a pressure plate switch (or a very large push button) it is time to connect it to your Arduino. A pressure plate switch is a really big temporary switch or push button. To connect one of these things to your Arduino you going to need:

  • An Arduino or compatible
  • 10 K resistor
  • Breadboard
  • Jumper wire
  • Your completed pressure plate

Connecting your pressure plate is much like connecting any temporary switch/button to your Arduino. The following diagram shows what it would look like if you connect it to an Arduino Uno. 

As you see connecting it to your Arduino is a breeze.  Take your 10K resistor and connect it to 5v as seen on the diagram. Connect one lead of your Pressure Plate to ground and the other one to your 10K resistor as displayed in the diagram. Next connect a jumper wire from the 10K resistor to digital pin 2 on your Arduino.

The most complex part of this setup is that 10K resistor. This is what we call a pull up resistor. Its purpose is to eliminate false readings from you digital input pin on your Arduino. I am not going to go deeper into the workings of this pull up resistor as it falls out of the scope of this tutorial.

The Code for your Pressure Plate

Here is where the rubber meets the road. Although connecting your components correctly is paramount for a successful project, writing the code actually make things happen. One of the most basic things we handle is a push button. But lots of people are still do this wrong.

If you don’t create a debounce system in your code you get many erroneous inputs from this button. Without debouncing a button, one press of the button (or one step on the pressure plate) might be interpreted by the microcontroller as many button presses. This is because of the mechanical properties of these push buttons and the speed the microcontroller reads the inputs. 

Definition of Debouncing
Electrical contacts in mechanical pushbutton switches often make and break contact several times when the button is pushed. A debounceing script removes the resulting ripple signal, and provides a clean transition at its output

The way we are going to debounce the button is by counting milliseconds. We set how many milliseconds we want to wait until we allow the next action from a button. To do this we use the millis() function.

The millis() function basically returns in milliseconds how long your sketch has been running. After about 50 days the millis() number has become so big that it returns to 0 and the fun start all over again. 

How does this work?

We create two variables:

unsigned long lastDebounceTime = 0; 
unsigned long debounceDelay = 1000;

The debounceDelay variable tells the sketch how long you want to wait before the next action (in milliseconds). The lastDebounceTime variable gets a new value every time the button gets pressed, and the wait time set by the  debounceDelay variable has expired. It uses the millis() function to get its new value. 

if ((millis() - lastDebounceTime) > debounceDelay) {
      lastDebounceTime=millis();
      action();
       }

I have created two example sketches. One that will activate your project when somebody steps on the plate (step_on.ino) and one that will activate your project when stepping off the plate (step_off.ino). You can download them by clicking on the links below

In the sketch you only need to edit the debounceDelay value to your preferred delay time (in milliseconds).  You copy your code in the action() function found at the bottom of the sketches provided. Don't forget to also copy your includes and variables you have created to the top of the sketch, and modify the setup() function to reflect your needs.

void action(){
    //your code goes here
    }

Using a motion sensor (Passive Infrared (PIR) Sensor)

A motion sensor operates much like a button. If somebody is in the line of sight of the infrared beam it sends a signal to the digital pin on your Arduino (pulls the pin high). Lets take a closer look at how to integrate your PIR sensor into your project.

Materials needed:

  • An Arduino or compatible
  • Jumper wire
  • A PIRmotion sensor

As you see in the diagram it is a very simple process to connect your motion sensor. You connect the power in lead to 5V and the ground lead to gnd on the Arduino Uno. The signal lead goes to digital pin 2 on your Arduino, and your sensor is ready for some code.

How does the code works

The sketch for the motion sensor works much the same as the sketches for the pressure plate switch. We use the same variables and also include the action() function to copy your code in. You can download the sketch named pir_sensor.ino here.

In this example we use the debounceDelay variable to delay the activation of your project not because of electrical noise, but to stop the project from constantly working because people stand in the beam

Now you just need to copy your code in the appropriate places (the action() function) and alter the debounceDelay variable to your liking. If you bought a PIR sensor with adjustments you can tune down the sensitivity of your sensor. The sensor I am using doesn't have that capability. To reduce the sensitivity of my sensor I created a protection hood that only allows a small amount of the infrared beam to return to the sensor.

If you like this tutorial and want to see more of this please If you are interested subscribe to my periodical Newsletter by filling out the form below, or follow me on Facebook by clicking on this link, and click the follow button. 

Subscribe to our mailing list

* indicates required
Email Format
Read More
Helpful Tips Ab Kurk Helpful Tips Ab Kurk

Helpful Tip: How to create a more professional and user friendly Arduino project by storing your configuration in the EEPROM

As makers we strive to make projects that are easy to use and have a more professional feel about them. If your project is user configurable (e.g. are you using C or F for your temperature), how do you store that information so it is kept, even when the Arduino gets reset?

 What if you could make your Arduino remember these types of user changeable settings (e.g. using a menu saving your Wi-Fi password)?

Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction
The-Arduino-Maker-Man-helpfull-tips.jpg

As makers we strive to make projects that are easy to use and have a more professional feel about them. If your project is user configurable (e.g. are you using C or F for your temperature), how do you store that information so it is kept, even when the Arduino gets reset?

 What if you could make your Arduino remember these types of user changeable settings (e.g. using a menu saving your Wi-Fi password)? The solution is using the EEPROM library. This library allows you to store your configuration data like your WIFI router name and password, and be able to change it if you get a new router or password. The drawback is that you can only write and erase data 100,000 times so be careful to only write the most important data. Luckily you can read this data as many times as you want without any issue.

To write your data you use the; EEPROM.write(address, value), and to read your data you use the EEPROM.read(address). The drawback is that writing anything other than an integer can be complicated.

In the next weeks, I will be writing a tutorial on how to write your configuration or other information to your Arduino/Compatible so you too can create professional looking projects.If you are interested subscribe to my periodical Newsletter by filling out the form below, or follow me on Facebook by clicking on this link, and click the follow button. 

Subscribe to our mailing list

* indicates required
Email Format
Read More
Tutorial Ab Kurk Tutorial Ab Kurk

Tutorial: Using Interrupts to improve the functionality of your Arduino projects

A couple of weeks back I wrote a short tutorial on using timers instead of delay() functions to make your Arduino projects more responsive to input from buttons and sensors. Using interrupts is a different way to achieve the same result. By attaching an interrupt to a digital pin the Arduino will halt what it is doing to handle the input as it happens making your project more responsive. 

EXPLAINING WHAT AN INTERRUPT IS AND DOES.

Let’s say you are listening to your music with your noise canceling headphones on. At that point somebody calls you on your cell phone. Of course you don’t hear your phone ring as your music is loud and you are distracted by it. Luckily there is someone in the room with you who hears the ringing. This person pokes you in the side to let you know that your phone is ringing and tells you can answer it.

Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction

A couple of weeks back I wrote a short tutorial on using timers instead of delay() functions to make your Arduino projects more responsive to input from buttons and sensors. Using interrupts is a different way to achieve the same result. By attaching an interrupt to a digital pin the Arduino will halt what it is doing to handle the input as it happens making your project more responsive. 

Explaining what an interrupt is and does.

Let’s say you are listening to your music with your noise canceling headphones on. At that point somebody calls you on your cell phone. Of course you don’t hear your phone ring as your music is loud and you are distracted by it. Luckily there is someone in the room with you who hears the ringing. This person pokes you in the side to let you know that your phone is ringing and tells you can answer it.

An interrupt works much the same. Your Arduino is distracted by running the code you wrote. It might not notice inputs from buttons or sensors. The interrupt is the person in the room that tells the Arduino to respond to the button press. 

Most Arduinos have Interrupts attached to hardware pins. The table below has a list of Arduino’s and what pins have interrupts attached to them. If your board is not on this list just check the specs of your board to see what pins have an interrupt attached to them.

Board

int.0

int.1

int.2

int.3

int.4

int.5

Uno, Ethernet

2

3

 

 

 

 

Mega2560

2

3

21

20

19

18

32u4 based (e.g Leonardo, Micro)

3

2

0

1

7

 

You can attach an input sensors or buttons to one of those pins. When the sensor/button provide input to one of the interrupt pins your Arduino will halt what it is doing and run the code you attached to that pin.

The Code

In your setup() function you would have a line to attach the interrupt to the pin.

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
  • pin: is the number of your hardware pin you want to use
  •  ISR: is the function that contains the code that you want to run
  •  Mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:
  •  LOW to trigger the interrupt whenever the pin is low,
  • CHANGE to trigger the interrupt whenever the pin changes value
  • RISING to trigger when the pin goes from low to high,
  •  FALLING for when the pin goes from high to low.

Here is a simple snippet of code that you will find when looking for this topic online. It connects a pushbutton to pin 2 of an Arduino Uno. When you press the button the led on pin 13 will either turn on or of

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink1, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink1() {
  state = !state;
}

Notice the variable state has the volatile property added to it. This stores the variable in a memory space so that when changed by the interrupt function it gets changed throughout the whole sketch. It will become clear why that is required later on.

Example

To illustrate the effect of the Interrupts we are going to create a project that has 4 LED's that will blink in sequence with a one second interval between them. You will have a push button that will light all the lights at once when pressed. Below is a list of materials you need. You also have to download the two example files.

interrupt_demo1.ino uses a normal digitalRead() to look for the input from the button. In this example it can take up-to 4 seconds before the button will execute. In the interrupt_demo2.ino we use the attachInterrupt() function to read the button input. In this case the button responds instantly.

Downloads:

Fritzing Bill of Materials

Part List

Amount Part Type Properties
2 Red LED color Red ; package 5 mm [THT]; leg yes
2 Yellow LED color Yellow ; package 5 mm [THT]; leg yes
1 Arduino Uno (Rev3) - ICSP type Arduino UNO (Rev3) - ICSP
4 330Ω Resistor resistance 330Ω; tolerance ±5%; bands 4; package THT
1 Pushbutton package [THT]



In the first example we use the button without the interrupt attached on on pin 2 and use the conventional way to look at inputs. When pressing the button you will experience delays in execution and it will seem the button is not functional. You will notice that I don't use a pull up resistor on the button. We use the internal pull up resister on pin 2 with by using this code:

pinMode(interruptPin, INPUT_PULLUP);

In example 2 we use the interrupt connected to pin 2. The respoce of the button press is almost instantaneously. When the interrupt is triggered by pressing the button the Arduino will execute the code in the function blink1(). The moment that code has been executed it will return  to where it previously left  off.  This is why I have the if statement (as you see below) behind every delay to bring us back to the beginning of the loop. 

if(state==LOW)return;

Without the if statement the state variable would have changed it's value, but the program would not use this until it ran through all the code and would reach the top of the loop.

In conclusion

The attachInterrupt() function works great for buttons, but in practice it gets used more for input from sensors like IR receivers, or motion sensors. Be aware to use the volatile property for global variables that get changed by the interrupt.

Also remember that the program will resume where it left off when the code in the interrupt function has finished. If you want to change that you need to add conditional statements that alter the path of your code. 

 

 

 

Read More