Getting started with NodeMCU ESP8266

I knew when I started all of this that my end goal was going to be working with micro-controllers.  If you’ll recall my very first post you’ll know that I bought this NodeMCU ESP8266 board from Amazon for something like $8 which I thought was a great deal.  Now that I’ve had a chance to play with it, Im totally blown away with how easy this is to do considering that these boards now work with Arduino IDE as well.

Note: Im doing all of this on a Mac but the steps should apply to any OS.  Just make sure you download the OS specific drivers and apps.

In this post, we’re going to get the board up and running.  The first thing you need to do is head over and download the USB drivers you’ll need to talk to the board.  More specifically, without these, your computer wont see the board to talk to it.  The drivers are available here.

After that – it’s just a matter of installing the Arduino IDE.  To do that head over here.  Pick the link to download the full blown IDE.  Once downloaded, you should be able to just run it like any other applications.  Once running, there are a couple of things you’re going to need to get Arduino to work with the board.  The first is to download the board through the board manager.  To do that head over to the preferences window (under either File or ‘Arduino’ depending on OS) and add this URL as an ‘Additional Boards manager URL’…

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Once added you can click OK to close the preferences window.  Now we need to add the board under the board manager.  To do that head over to the Tools menu, then down to Boards, and on the context menu click on the ‘Board Manager’ option…

In the search bar search for ‘esp8266’ and you should see an option for ‘esp8266 by esp8266 community’ …

Mine shows already installed since Im running it but yours wont so click the install link.  Once installed go ahead and setup your board.  For our first experiment, we’re going to press a button on the board which will then turn on an LED for us.  To do that, setup you’re board as follows…

Note: Missing series LED resistor in this picture for some reason…

You might need to zoom in to see the pinouts.  Oh fine…  I’ll draw it out too…

Ok – so this doesnt look so bad.  And it even seems to make some sort of sense!  So let’s look at the Arduino code we’re going to use…

So let’s walk through this.  There appear to be two main functions – setup and loop.  For now let’s just say that things in the setup function run once right when the device turns on.  Things in loop… well loop… indefinitely.  Above that we have the ability to define some variables.  In our case, we’re assigning the button pin and LED ID to a variable.  More on that below…

So in setup we’re doing a couple of things.  We first setup the capability to send messages to the serial monitor (we’ll show that later).  Then we tell the program what pins we want to use for what.  In our case, we’re using the D1 and D2 pins on the board.  However – you need to reference this mapping to see what number those pins are…

This diagram was found on the NodeMCU Github page.   So it looks like pins D1 and D2 reference GPIO 5 and 4 respectively.  So in the code, all we’re doing is saying that pin 5 will be used for the button (input) and pin 4 will be used for the LED (output).  Pretty easy right?

Then we get right into the loop.  In the loop we check the state of the button by using the digitalRead function.  This should return either HIGH or LOW.  If the button is pressed, we should get a reading of HIGH.  If it’s not, we should get a value of LOW.  The rest of the code is the logic to tell the LED to turn on when the state reads as HIGH.  It uses the digitalWrite function to tell a pin what to do.  In this case, go HIGH or LOW.

As you can imagine, if this code is running in a loop it will just keep checking the state.  If it’s HIGH for one loop iteration it will turn the LED on.  If it’s low the next loop iteration, it will turn the LED off.

So what does HIGH and LOW mean?  Well, the GPIO pins can read their voltage.  So if we look at our digram again we can dig in a little further.  The LED part is easy.  That’s just an LED with a series resistor calculated for this LED which is using a 3.3 volt power source and drawing 50 milliamps (Im wondering if I did that math wrong…. yeah, that’s the wrong kind of LED…. so make sure you use the right resistor for your component unlike I did).  When the D2 pin gets power it sends 3.3 volts to the LED and it turns on.

The button takes some more explaining.  Notice how the connection from the board pin D1 toward the switch has a resistor in path grounding that connection.  What’s that all about?  Looking closely you’ll notice that the other end of the button is hooked up to the positive side of the 3.3 volt power supply.  So if I push the button won’t I be shorting the circuit?  Yes and no.  This 10k resistor is called a pull-down resistor.  It’s job is to give the pin D1 a reading when the button is open.  In this case, ground.  So when the button is open, the D1 pin knows it’s getting ground.  When I push the button I connect the positive side of the power supply to the D1 pin and also ground.  But the connection to ground is through a resistor.  A really big resistor in fact.  So while Im passing current toward ground, it’s really nothing at all.  So the job of the pull-down resistor is to give the D1 pin a steady reading.  Without this, the pin would be said to be floating and could really read either HIGH or LOW.

So copy and paste that code into the Arduino console and then make sure you’re set to the right board and port…

If the USB serial port doesn’t show up for you then you likely have driver problems.  Did you install the USB driver like I mentioned up above?

Once you have those two options set – go ahead and send the code by pressing the big green arrow button…

You should see the program doing its work and uploading the code in the bottom window…

Once it’s done you should be up and running!  Go ahead and try to push the button…

The let go of the button and the LED should turn off…

Nice!  It works!  We can also see that the board is sending information to the serial console when we push the button.  Open that up under Tools, serial monitor…

Now here’s some extra credit work.  How could we reverse this functionality without changing the code?  Recall that the code is looking for a HIGH condition to turn the LED on.  So ‘if not HIGH’ would be LED off.

Ok – I wont leave you hanging.  Change the pull-down resistor to a pull-up resistor.  That is, move the leg of the resistor that was in ground to the positive side of the power rail and then put the side of the switch that was in the positive side of the power rail into the ground.  Doing this reverses the equation for us.  Since the resistor is now ‘pulling up’ the reading of D1 and pushing the button shorts it to ground.  Pretty easy huh?  I cant wait to keep playing with this board.  I cant get over how easy the Arduino IDE is.

 

Leave a Reply

Your email address will not be published. Required fields are marked *