Reader small image

You're reading from  ESP8266 Internet of Things Cookbook

Product typeBook
Published inApr 2017
PublisherPackt
ISBN-139781787288102
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
Marco Schwartz
Marco Schwartz
author image
Marco Schwartz

Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has a master's degree in electrical engineering and computer science from Supélec, France, and a master's degree in micro engineering from the Ecole Polytechnique Fédérale de Lausanne (EPFL), Switzerland. He has more than five years' experience working in the domain of electrical engineering. Marco's interests center around electronics, home automation, the Arduino and Raspberry Pi platforms, open source hardware projects, and 3D printing. He has several websites about the Arduino, including the Open Home Automation website, which is dedicated to building home automation systems using open source hardware. Marco has written another book on home automation and the Arduino, called Home Automation With Arduino: Automate Your Home Using Open-source Hardware. He has also written a book on how to build Internet of Things projects with the Arduino, called Internet of Things with the Arduino Yun, by Packt Publishing.
Read more about Marco Schwartz

Right arrow

Chapter 2. Your First ESP8266 Projects

In this chapter, we will cover:

  • Functionalities of the ESP8266

  • Reading digital signals

  • Reading analog signals

  • Controlling an LED

  • Dimming an LED

  • Controlling a servo motor

  • Measuring data from a digital sensor

  • Controlling an OLED screen

  • Troubleshooting usual issues with ESP8266 basics

Introduction


In this second chapter of the book, we will learn how to use the General Purpose Input/Output (GPIO) pins to read input signals and produce output signals. With that knowledge,we will create simple projects to control outputs and read sensor inputs. This will give way to the development of more complex projects in later chapters.

Functionalities of the ESP8266


The first thing we will tackle is the functionality of the ESP8266. We will look at the features and functions of the ESP8266, and learn about all the available GPIO pins and how we can use them to read and write digital signals.

Features

The ESP8266 chip provides a self-contained, standalone Wi-Fi networking solution that allows you to host applications and to provide a Wi-Fi network that you can use to transfer data and instructions in your projects.

In normal use, the ESP8266 boots up directly from an external flash memory. It also has a cache that improves the performance of the system and reduces the amount of memory needed during operation.

The ESP8266 can also be used as a Wi-Fi adapter. In this case, the chip provides wireless Internet access to a microcontroller-based project through either the CPU AHB bridge or UART interface.

The on-board processing and storage capabilities of the ESP8266 enable easy integration with sensors and other devices via the...

Reading digital signals


One of the uses of the ESP8266's GPIO pins is to read digital signals. This allows you to control your project using input devices and also to monitor sensor data. In this recipe, we will look at how to read digital signals using GPIO pins.

Getting ready

Connect your ESP8266 board to your computer via a USB cable and set up your Arduino IDE (refer back to Chapter 1, Configuring the ESP8266). Once that is done, you can proceed to make the other connections.

In this recipe, we will need a few extra components. They include:

  • Breadboard

  • Jumper wires

Mount the ESP8266 board onto the breadboard and then connect a jumper wire from pin 5 to the GND pin. The connection should be as shown in the following figure:

How to do it…

We will configure pin 5 as an input, then read it using the digitalRead() function and display the state of the input signal on the serial monitor. This will be repeated every 1 second:

// LED pin
int inputPin = 5;
int val = 0;

void setup() {
  Serial.begin(9600...

Reading analog signals


The ESP8266 has one analog pin that we can use to read analog signals. In this recipe, we will be looking at how to write a sketch that reads analog signals. This will enable us to read input from analog sensors.

Getting ready

Connect your ESP8266 board to your computer via a USB cable and set up your Arduino IDE (refer back to Chapter 1, Configuring the ESP8266). Once that is done, you can proceed to make the other connections.

In this recipe, we will need a breadboard and jumper wires in addition to the ESP8266 board. Mount the ESP8266 board onto the breadboard and then connect a jumper wire from the analog ADC pin to the GND pin. The connection should be as shown in the following diagram:

How to do it…

We will use the analogRead() function to read the analog signal on the ADC pin and display the analog signal value on the serial monitor. This will be repeated every 1 second:

// LED pin
int val = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  // read pin...

Controlling an LED


This recipe is going to look at how to control an LED using an ESP8266 board. This will basically involve changing the state of an LED either ON or OFF, using the ESP8266 board's GPIO pins. The exercise will enable you to understand how to use the digital output function on the ESP8266.

Getting ready

Connect your ESP8266 board to your computer via a USB cable and set up your Arduino IDE (refer back to Chapter 1, Configuring the ESP8266). Once that is done, you can proceed to make the other connections.

In this recipe, we will need the following components:

Start by mounting the LED onto the breadboard. Connect one end of the 220 Ω resistor to the positive leg of the LED (the positive leg of an LED is usually the taller one of the two legs). Connect the other end of the resistor to another rail of the breadboard and connect one end of...

Dimming an LED


In this section, we are going to look at how to dim an LED using the analog output function. This will involve controlling the brightness of an LED that is attached to one of the GPIO pins of the ESP8266.

Getting ready

You will need an ESP8266 board and a USB cable to do this tutorial. There will also be some additional components required for this project. They include:

Start by mounting the LED onto the breadboard. Connect one end of the 220 Ω resistor to the positive leg of the LED (the positive leg of an LED is usually the taller one of the two legs). Connect the other end of the resistor to another rail of the breadboard and connect one end of the jumper wire to that rail and the other end of the jumper wire to pin 4 of the ESP8266 board. Take another jumper wire and connect one of its ends to the negative leg of the LED and connect the other end to the...

Controlling a servo motor


In this section, we are going to look at how to use the functionalities of the ESP8266 to control a servo motor. We will be rotating the servo motor to a specific position in either direction at a set speed. This will demonstrate the different servo motor parameters we can control using an ESP8266 board.

Getting ready

Make sure you have all the components before proceeding. They include:

Start by mounting the ESP8266 board onto the breadboard. Use jumper cables to connect the power wires of the servo to the ESP8266 power pins. The power wires of the servo are coloured red for the positive terminal and black/brown for the negative terminal.

Connect the positive terminal to the USB pin of the ESP8266 board and the negative terminal to the GND pin of the ESP8266 board. We connect the positive terminal to the USB pin because it provides 5V, which is within the recommended...

Measuring data from a digital sensor


ESP8266 boards can be used to read and monitor data from both digital and analog sensors. To demonstrate this, we are going to measure data from a digital temperature and humidity sensor then display the data on the serial monitor.

Getting ready

In this tutorial, you will need an ESP8266 board, a USB cable, and a few other components, which include:

The DHT11 pin configuration is shown in the following diagram:

First mount the ESP8266 board and the DHT11 sensor onto the breadboard. Connect a 10 kΩ pull up resistor to the DHT11 data pin and connect the VCC pin and GND pin to the 3.3V pin and GND pin of the ESP8266 board, respectively. Finally, connect the data pin of the DHT11 to GPIO 2 of the ESP8266 board. Use jumper wires to do the connections.

The setup is shown in the following diagram:

How to do it…

To measure temperature and humidity readings...

Controlling an OLED screen


To make your ESP8266 projects more interactive, you have to use more comprehensive ways of displaying data and information. This can be achieved with an OLED screen. Using it, you will be able to display writing and images, enabling one to know the current state of the project and to monitor its operations. In this recipe, you will learn how to control an OLED screen to display information on an ESP8266 project.

Getting ready

You will need an ESP8266 board, a USB cable, and a few other components, which include:

The monochrome 1.3" 128x64 OLED graphics display pin configuration is shown in the following diagram:

The display has two interfaces that you can use to communicate with the ESP8266 board. They are i2C and SPI interface. When using the i2C interface, the pin connections should be as follows:

  • GND of the display goes to GND of the ESP8266 board

  • Vin of the display...

Troubleshooting basic ESP8266 issues


You may run into some issues when using the ESP8266's basic functions. We will list some of the common problems that many people face and some ways of troubleshooting them.

The analog pin cannot measure high voltages

The analog pin of the ESP8266 can only measure voltages between 0V and 1V. If you have a sensor that outputs an analog signal that goes above that range, you will need to divide it. Otherwise, most of your readings from the analog pin will be 1023.

The best way of dividing the voltage is by using a voltage divider. It is easy to implement, since all you need is two resistors of the desired value and you are good to go.

Since most of the analog sensors you use with the ESP8266 board will output a signal of voltages between 0V and 3.3V, you will need a 1200 Ω resistor (R1) and a 470 Ω resistor (R2) to build a voltage divider.

You can read more on voltage dividers at this link: https://learn.sparkfun.com/tutorials/voltage-dividers.

The board stops...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
ESP8266 Internet of Things Cookbook
Published in: Apr 2017Publisher: PacktISBN-13: 9781787288102
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Marco Schwartz

Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has a master's degree in electrical engineering and computer science from Supélec, France, and a master's degree in micro engineering from the Ecole Polytechnique Fédérale de Lausanne (EPFL), Switzerland. He has more than five years' experience working in the domain of electrical engineering. Marco's interests center around electronics, home automation, the Arduino and Raspberry Pi platforms, open source hardware projects, and 3D printing. He has several websites about the Arduino, including the Open Home Automation website, which is dedicated to building home automation systems using open source hardware. Marco has written another book on home automation and the Arduino, called Home Automation With Arduino: Automate Your Home Using Open-source Hardware. He has also written a book on how to build Internet of Things projects with the Arduino, called Internet of Things with the Arduino Yun, by Packt Publishing.
Read more about Marco Schwartz