9 The Bonus Chapter: The Emergency Backup Battery Power System, Power Intensive Related Projects, Using the NodeMCU with an ArduCAM Mini Camera, and Some Important Downloads – A DIY Smart Home Guide: Tools for Automating Your Home Monitoring and Security Using Arduino, ESP8266, and Android

CHAPTER 9

The Bonus Chapter: The Emergency Backup Battery Power System, Power Intensive Related Projects, Using the NodeMCU with an ArduCAM Mini Camera, and Some Important Downloads

IN THIS CHAPTER I COVER some topics that I believe are important but were not covered previously in the book. Most of this chapter focuses on power intensive related Arduino projects that require a separate power source for the device being controlled. These projects involve a servo, a DC motor with fan attachment, a stepper motor, and a smoke sensor. An emergency backup battery power system for the Arduino is also presented. For the downloadable content for this chapter I have included all the Android APK install files for each version of the Android basic wireless frameworks. I have also included an Android Studio project of the final version of the Android basic wireless framework that was created with a more recent version of Android Studio. I also give some troubleshooting tips for readers that are having trouble getting the examples to work correctly.

Circuit Troubleshooting Tips

The two most common problems that I have encountered when building the projects in this book are poor electrical connections between the jumper wires and the circuit board and USB power cables that are too tight fitting and provide poor electrical contacts for power transmission. I especially found that many USB cables I tried with the official Arduino Mega 2560 were too tight fitting and thus had problems providing adequate power to the Arduino.

Some signs of poor electrical contacts between jumper wires and the breadboard or between the USB cable and the Arduino are:

   Garbage characters appearing on the Serial Monitor.

   Fluctuating or dim power LED lights on components such as the ESP-01.

   No power to the Arduino or components attached to the Arduino such as the ESP-01 or step-down voltage regulator.

   A component in the circuit works intermittently, sometimes working perfectly and other times failing completely.

One solution to wires that are making poor electrical contact would be to shut off the power, wiggle the wires slightly, and then turn the power back on and see if this has solved the problem. For USB cables that are too tight fitting the best solution would be to try a different one. Many clone Arduino boards come with short USB cables and you can buy USB cables online from Amazon and ebay.

Android Basic Wireless Framework APKs

For the convenience of the reader I have collected all the executable Android APKs for the Android basic wireless framework versions in this book which are 1.0, 1.1, 1.2, 1.3, 2.0 (Android Studio 1.5), and 2.0 (Android Studio 2.3.1). All these files should be located on the download site provided by McGraw-Hill for this chapter.

Converting the Android Basic Wireless Framework Version 2.0 Project from Android Studio 1.5 to Android Studio 2.3.1

The Android projects in this book were mostly created using Android Studio version 1.5. For this chapter I converted the Android basic wireless framework version 2.0 application project from Android Studio 1.5 to Android Studio 2.3.1. What I did was using the newer Android Studio version 2.3.1 I first created a new blank application that had no activity. I copied the source code, resource files, sound effect files, and manifest from the Android Studio version 1.5 project folders to the corresponding Android Studio 2.3.1 project folders. I also had to change the package name in the files to match the new package name for the project. Figure 9-1 shows some of the files that I needed to modify or copy over identified by a red circle.

Figure 9-1   Converting to Android Studio 2.3.1.

For this new version of Android Studio there are two files, called colors.xml and styles.xml, that are either new or have been changed from Android Studio 1.5. Both files are located under the “values” directory in the project tree. See Figure 9-2. The files have been circled in red.

Figure 9-2   Converting to Android Studio 2.3.1.

Finally, I changed the MainActivity class to derive from the AppCompatActivity class instead of the Activity class.

The Automatic Battery Backup Power Supply System

The purpose of this automatic battery backup power supply system is to provide a reliable electrical power source from a 120 VAC receptacle through a 120 VAC to 9 VDC adapter to an Arduino as a primary power source. A reliable backup power source of a battery pack of 9 VDC is provided in case of a power failure or failures. If a power failure or multiple power failures or power interruptions should take place, this superfast solid-state automatic power backup system will connect the backup battery power source to the Arduino unit until the primary power has been restored to normal. When the AC power has been restored the Arduino will be returned to the normal primary power source instantaneously and without any lost operational time.

To understand how this automatic battery backup power supply system functions please refer to Figure 9-3. On this diagram it has two power sources. One power source is from the 120 VAC to 9 VDC adapter output. This 9 VDC output from the adapter is always greater than 9 VDC (usually about 12 VDC with open circuit and not connected to an Arduino). There are two superfast switching Schottky diodes. When the Arduino is connected as shown in Figure 9-3 and energized, the sequence of events is as follows:

Figure 9-3   The battery backup power supply system schematic.

1.   The Schottky diode D1 will be conducting as the 9 VDC from the 120 VAC adapter is higher than 9 VDC from the backup battery.

2.   As the Schottky diode D1 conducts, this provides the +9 VDC as the power source for the Arduino and also provides the isolation between the backup battery power source and the +9 VDC adapter power source. A voltage of +9 VDC is placed at the cathode of Schottky diode D2 to ensure that D2 is in cut-off status.

3.   If and when the primary power source fails, the superfast switching Schottky diode D1 ceases to conduct instantaneously and the Schottky diode D2 simultaneously conducts to provide a backup power source to the Arduino.

4.   At the instant the primary power has been restored, the Schottky diode D1 starts conducting again and providing power to the Arduino unit at the same time cutting Schottky diode D2 off completely because the output voltage from the 9 VDC adapter is higher than the output voltage from the 9 VDC backup battery.

See Figure 9-3.

Parts List

Note 1: Please choose one or two of the following backup battery source or sources primarily for the length of time that you want the backup battery to support the Arduino unit to continue operating without any interruption after a power failure or power interruption has taken place.

1a: A 9-V Energizer battery with 565 mAh capacity that lasts about 30 minutes.

1b: A 9-V Energizer battery pack with six “C” cells of 1.5 V each that have a capacity of 5000 mAh and last greater than 16 hours of operation.

1c: A 9-V Energizer battery pack with six “D” cells of 1.5 V each that have a capacity of 10,000 mAh and will last about 28 hours.

Note 2: Item 1 and item 5, part #416A4136 and part #NTE 585, above may be purchased through the Internet from https://www.ebay.com. Items 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, and 14 may be purchased through the Internet from https://www.amazon.com.

Building the System

1.   Begin with item #6 from Parts List, have the Global Specialties breadboard in front of you with the black terminal post on your right-hand side.

2.   Select item #4 and place the 9-V battery holder on the left end of the breadboard. Next, using a 3/16-inch long small screw mount the battery holder on the end of the breadboard.

3.   Take one Schottky diode, item #5, with the silver band end, insert its pig tail into the breadboard hole located at “+ (positive), 20” (row +, column 20) just above the red line of row A, column 25. The opposite end of the pig tail is to be inserted into “i, 25” hole (row i, column 25).

4.   Take another Schottky diode, item #5, with the silver band end, insert its pig tail into the breadboard hole located at “+, 32” (row +, column 32) just above the red line of row A, column 40. The opposite end of the pig tail is to be inserted into “i, 40” hole (row i, column 40).

5.   Select item #8, the Adafruit jack. The rear center pin on the jack must be in line with the hole “C, 58” (row C, column 58). At the same time the middle center pin on the jack must be in line with hole “C, 60” (row C, column 60). Gently push the jack body on to the breadboard ensuring that the anchoring pin on the right side of the jack is secured in place. However, if needed, apply a drop of super glue to the corners of the jack that are in contact with the breadboard.

6.   Select item #13, a jumper wire, insert one end of the wire into hole “A, 58” and the other end into the hole “− (negative), 45” (row − (negative), column 45).

7.   Take another jumper wire and insert one end of the wire into hole “− (negative), 46” (row − “negative,” column 46) and the other end to the black terminal post and secure it.

8.   Take another jumper wire and insert one end of the wire into hole “+ (positive), 36” (row +, column 36) and the other end of the wire to the red terminal post and secure it.

9.   Take another jumper wire and insert one end of the wire into hole “A, 60” (row A, column 60) and the other end into hole “J, 40” (row J, column 40).

10. Select item #7, adapter barrel plug with pig tail black and red leads, insert the black lead into the black terminal post along with the existing wire already attached, and then secure the black terminal. And insert the red lead into the red terminal post along with the existing wire already attached, and then secure the red terminal post.

11. Select items #2, #3, #4, take one 9-V battery clip, and line it up with the 9-V battery. Next, push the battery clip on to the battery and install the battery into the 9-V battery holder. Then take the black wire from the 9-V battery clip and insert the end of the wire to hole located “− (negative), 20” (row −, column 20), and take red wire from the 9-V battery clip and insert the end of the wire to hole located “j, 25” (row j, column 25) on the breadboard. This rectangular 9-V battery has a capacity of 565 mAh and lasts for about 30 minutes of Arduino operation. See Figure 9-4.

Figure 9-4   Building the automatic battery backup power supply system.

A. To choose a 9-V backup battery with a larger capacity like a “C” cell battery pack with 5000 mAh that will last about 16 hours, you simply remove the two wires (the black and the red wire) from the present existing hole locations and install the two new wires from the 9-V “C” cell battery pack in place. Hole locations are “−, 20” (row −, column 20) for black wire and “j, 25” (row j, column 25) for the red wire.

B. A 9-V backup battery with a higher capacity like a “D” cell battery pack with 10,000 mAh will last about 28 hours. For the proper output wiring hook up from the “D” cell battery pack, take the black wire and insert it in hole location “−`, 20” (row −, column 20) and take the red wire and insert it in hole location “ J, 25” (row J, column 25).

12. To make the C cell battery pack select items #9, #10, and #13 and insert each of the 6 “C” cell 1.5-V batteries into the “C” cell battery holder. Be sure to observe the proper polarity of the batteries accordingly with the battery holder. Take a jumper wire (preferably black color) and attach one end of the wire to the “−” (negative) output terminal of the battery holder. Secure it with a strip of vinyl electrical tape. The other end of the jumper wire remains free (no connection). Take another jumper wire (preferably red color) and attach one end of the wire to the “+” (positive) output terminal of the battery holder and secure it with a strip of vinyl electrical tape. The other end of the jumper wire remains free (no connection). The 9-V “C” battery pack has about 5000 mAh and may last for about 16 hours. See Figure 9-5.

Figure 9-5   The C cell battery pack.

13. To make the D cell battery pack select items #11, #12, and #13 and insert each of the 6 “D” cell 1.5-V batteries into the “D” cell battery holder. Be sure to observe the proper polarity of the batteries accordingly with the battery holder. Take a jumper wire (preferably black color) and attach one end of the wire to the “–” (negative) output terminal of the battery holder and secure it with a strip of vinyl electrical tape. The other end of the jumper wire remains free (no connection). Take another jumper wire (preferably red color) and attach one end of the wire to the “+” (positive) output terminal of the battery holder and secure it with a strip of vinyl electrical tape. The other end of the jumper wire remains free (no connection). The 9-V “D” battery pack has about 10,000 mAh and may last about 28 hours. See Figure 9-6.

Figure 9-6   The D battery pack.

The AC adapter, which converts power from 120 V AC into 9 V DC power, is shown in Figure 9-7.

Figure 9-7   The 120-V AC to 9-V DC power adapter.

The entire backup battery power system with the C cell and the D cell battery packs along with the AC adapter is shown in Figure 9-8.

Figure 9-8   The entire backup battery power system with battery packs.

The SG90 9g Micro Servo

The SG90 9g micro servo is a powerful servo that can be used in applications such as robotics, model airplanes, and home security. The specifications of this servo are as follows:

   Operating voltage: 4.8 to 6 V

   Operating temperature: 0 to 55 degrees Centigrade

   Idle current: 6 mA (+ or −10 mA)

   Operating current (no load): 220 mA (+ or −50 mA)

   Operating current (maximum load): 650 mA (+ or −80 mA)

   Vcc connector color: Red

   Gnd connector color: Brown

   Servo input connector color: Orange/Yellow

See Figure 9-9 for a picture of the servo.

Figure 9-9   The SG90 9g micro servo.

The YwRobot 545043 Power Supply Module for a Breadboard

The SG90 micro servo draws so much power when used in an actual situation containing a load that you need a separate power supply for the servo. The Arduino Mega 2560 can use at most 800 mA of current to power itself and the devices that are attached to it. This by itself is near the maximum current usage of the micro servo when under load. If you add in the power drawn by the ESP-01 you can easily see there is a need for an additional separate power source for the servo. One such power source is a power supply module that can be used with a breadboard and is made by YwRobot. This module is shown in Figure 9-10.

Figure 9-10   The YwRobot 545043 power supply module for a breadboard.

This power module has a DC barrel jack so that you can use a 9-V battery or a 9-V AC adapter to power the module. There is also a switch next to the DC barrel jack that is used to turn the power module on or off. This power module from YwRobot also has a selection jumper for the output of 5 or 3.3 V. To select the 5-V option set the jumpers to the configuration shown in Figure 9-11.

Figure 9-11   Selecting 5 V as an output from the power supply.

Hands-on Example: The Wireless Remote Control Servo System Using the Arduino Mega 2560 and ESP-01 Server

In this hands-on example I show you how to build a wireless remote control system that controls a micro servo using an Android, and Arduino Mega 2560 with an ESP-01 Wi-Fi module configured as the server.

Parts List

   1 Android cell phone

   1 Arduino Mega 2560 microcontroller

   1 5- to 3.3-V step-down voltage regulator

   1 Logic level converter from 5 to 3.3 V

   1 ESP-01 Wi-Fi module

   1 Arduino development station such as a desktop or a notebook

   1 Breadboard

   1 SG90 Servo

   1 Power supply module for a breadboard (such as the YwRobot 545043 Power Supply Module)

   1 Package of wires to connect the components

Setting Up the Hardware

Step-Down Voltage Regulator Connections

1.   Connect the Vin pin on the voltage regulator to the 5-V power pin on the Arduino Mega 2560.

2.   Connect the Vout pin on the voltage regulator to a node on your breadboard represented by a horizontal row of empty slots. This is the 3.3-V power node that will supply the 3.3-V power to the ESP-01 module as well as provide the reference voltage for the logic level shifter.

3.   Connect the GND pin on the voltage regulator to the GND pin on the Arduino or form a GND node similar to the 3.3-V power node in the previous step.

ESP-01 Wi-Fi Module Connections

1.   Connect the Tx or TxD pin to the Rx3 pin on the Arduino Mega 2560. This is the receive pin for the serial hardware port 3.

2.   Connect the EN or CH_PD pin to the 3.3-V power node from the step-down voltage regulator.

3.   Connect the IO16 or RST pin to the 3.3-V power node from the step-down voltage regulator.

4.   Connect the 3V3 or VCC pin to the 3.3-V power node from the step-down voltage regulator.

5.   Connect the GND pin to the ground pin or node.

6.   Connect the Rx or RxD pin to pin B0 on the 3.3-V side of the logic level shifter. The voltage from this pin will be shifted from 5 V from the Arduino to 3.3 V for the ESP-01 module.

Logic Level Shifter

1.   Connect the 3.3-V pin to the 3.3-V power node from the step-down voltage regulator.

2.   Connect the GND pin on the 3.3-V side to the ground node for the Arduino.

3.   Connect the B0 pin on the 3.3-V side to Rx or RxD pin on the ESP-01. This corresponds to the A0 pin on the 5-V side.

4.   Connect the 5-V pin to the 5-V power pin on the Arduino.

5.   Connect the GND pin on the 5-V side to the ground node for the Arduino.

6.   Connect the A0 pin on the 5-V side to Tx3 pin on the Arduino. This pin corresponds to the B0 pin on the 3.3-V side.

SG90 Micro Servo

1.   Connect the GND pin to the GND node for the Arduino.

2.   Connect the Vcc pin to the 5-V output on the power supply module on the breadboard.

3.   Connect the input pin to digital pin 7 on the Arduino.

YwRobot 545043 Power Supply Module for Breadboard

1.   Put the power supply module on the breadboard so that the power and ground pins line up with the power and ground columns on the breadboard.

2.   Make sure that the jumper selection for the output power pins you will be using are set to a 5-V output.

3.   Connect the 5-V output of the power supply module to the Vcc pin on the servo.

4.   Connect the GND on the power supply to the ground node for the Arduino.

See Figure 9-12.

Figure 9-12   The wireless SG90 micro servo system.

Setting Up the Software

The Arduino code for this project is based on the basic wireless framework for the Arduino with the ESP-01 configured as the server. In this section I will cover only the changes to the basic wireless framework needed for this project.

In order to use the servo with the Arduino we need to first include the Servo library in the Arduino sketch.

#include <Servo.h>

The servo itself is declared as a variable called Servo1 and the type is of the Servo class.

Servo Servo1;

The Serovo1Pin variable holds the Arduino digital pin that is connected to the servo’s input pin and will be used to control the servo. The default setting is digital pin 7.

int Servo1Pin = 7;

The CurrentAngle variable holds the current angle of the servo.

int CurrentAngle = 0;

The InitializeServo() Function

The InitializeServo() function initializes the servo by telling the Servo class which pin the servo is connected to by executing the attach() command. After the servo is attached to the Arduino’s digital pin the angle is then set to the CurrentAngle value which is 0 by issuing a write() command. Program execution is then halted for 1 second or 1000 milliseconds. See Listing 9-1.

Modifying the setup() Function

The setup() function is modified by adding the call to the function InitializeServo() which initializes the servo.

Modifying the CheckAndProcessIPD() Function

The code for the angleset command is added to the CheckAndProcessIPD() function. This command sets the angle of the servo to the value after the equals sign by doing the following:

1.   The angle value is parsed based on the position of the equals sign.

2.   The servo’s angle value is actually set by executing the Servo1.write (CurrentAngle) function with the value of the angle found in step 1.

3.   A return text string is sent to the Android controller indicating the value the servo was set to. The return text was sent by calling the SendTCPMessage() function.

See Listing 9-2.

The code for the angledelta command is added to the CheckAndProcessIPD() function.

The angledelta command increases or decreases the servo angle by doing the following:

1.   Parsing the command and determining the amount the current angle should be increased or decreased.

2.   Adding the delta amount to the current angle.

3.   Clamping the final angle to a range between 0 and 180 inclusive.

4.   Setting the servo to the angle determined from step 3.

5.   Sending a text string back to the Android controller indicating that the angle was changed to its current value.

See Listing 9-3.

The angleread command returns the current servo angle to the Android controller. See Listing 9-4.

Operating the Servo System

Download and install the Arduino servo project for this chapter on your Arduino Mega 2560. Connect a power source to the external power supply module located on the breadboard and turn on the power supply module by pressing down the on/off switch located next to the DC barrel jack. You should see a green light activate on the power supply. Next, start up the Arduino’s Serial Monitor. You should see something like the following initialization sequence:

Connect your Android phone to the Wi-Fi access point located on the ESP-01 module. Start up the Android basic wireless framework application and connect to the TCP server running on the ESP-01 module. The Android should connect to the TCP server and change the name of the device. In my case the name of my Android device is set to “lg”.

Next, read the current angle of the servo by sending an “angleread” command from the Android application. The current angle should be returned to the Android application and should be 0 degrees.

Put one of the plastic arms that should have come with the servo on the servo’s rotating knob. Next, send an “angleset=180” command to the Arduino through the Android application to set the servo angle to 180 degrees. The Arduino will receive the command, set the servo angle to 180 degrees, and then send a text string back to the Android indicating that the angle was set to 180 degrees. Test the servo’s movement again by setting the angle to 90 degrees. You should see the following on your Arduino’s Serial Monitor:

Next, increase the servo’s angle by 10 degrees by sending an “angledelta=10” command to the Arduino using the Android basic wireless framework. The servo’s angle should now be set to 100 degrees from 90 degrees.

Keep increasing the servo’s angle until it reaches 180 degrees and can go no farther.

Next, subtract 10 degrees from the current servo angle by sending the Arduino an “angledelta=-10” command from the Android application. The new servo angle should be 170 degrees. Keep sending this command until the angle reaches 0. Confirm that the servo moves each time this command is sent.

The DC motor that I use in this chapter operates within the range of 3 through 6 V, has two leads that connect to a power source, and a shaft that rotates at high speed. See Figure 9-13.

Figure 9-13   The DC motor.

In order to use the DC motor with the Arduino I use the L9110 DC motor driver chip. This chip has an operating voltage of 2.5 V up to 12 V. It can also provide a maximum of 800 mA of operating current to a motor. The operating temperature is between 0 and 80 degrees Centigrade. The chip itself has 4 pins on each side and has a small notch to indicate the top portion of the chip. See Figure 9-14.

Figure 9-14   The L9110 DC motor driver chip.

In order to test the DC motor with a load a small plastic fan was put on the tip of the DC motor’s shaft. See Figure 9-15.

Figure 9-15   Fan attachment for the DC motor.

Hands-on Example: The Remote Control Wireless DC Motor Control System Using the Arduino Mega 2560 with an ESP-01 Server

In this hands-on example I show you how to build and operate a remote control wireless system that will control a DC motor using an Arduino Mega 2560 and an ESP-01 server.

Parts List

   1 Android cell phone

   1 Arduino Mega 2560 microcontroller

   1 5- to 3.3-V step-down voltage regulator

   1 Logic level converter from 5 to 3.3 V

   1 ESP-01 Wi-Fi module

   1 Arduino development station such as a desktop or a notebook

   1 Breadboard

   1 DC motor that can operate on 5 V and up to 800 mA of current

   1 L9110 DC motor driver chip

   1 Plastic fan that can be attached to the shaft of the DC motor

   1 Power supply module for a breadboard (such as the YwRobot 545043 power supply module)

   1 Package of wires to connect the components

Setting Up the Hardware

Step-Down Voltage Regulator Connections

1.   Connect the Vin pin on the voltage regulator to the 5-V power pin on the Arduino Mega 2560.

2.   Connect the Vout pin on the voltage regulator to a node on your breadboard represented by a horizontal row of empty slots. This is the 3.3-V power node that will supply the 3.3-V power to the ESP-01 module as well as provide the reference voltage for the logic level shifter.

3.   Connect the GND pin on the voltage regulator to the GND pin on the Arduino or form a GND node similar to the 3.3-V power node in the previous step.

ESP-01 Wi-Fi Module Connections

1.   Connect the Tx or TxD pin to the Rx3 pin on the Arduino Mega 2560. This is the receive pin for the serial hardware port 3.

2.   Connect the EN or CH_PD pin to the 3.3-V power node from the step-down voltage regulator.

3.   Connect the IO16 or RST pin to the 3.3-V power node from the step-down voltage regulator.

4.   Connect the 3V3 or VCC pin to the 3.3-V power node from the step-down voltage regulator.

5.   Connect the GND pin to the ground pin or node.

6.   Connect the Rx or RxD pin to pin B0 on the 3.3-V side of the logic level shifter. The voltage from this pin will be shifted from 5 V from the Arduino to 3.3 V for the ESP-01 module.

Logic Level Shifter

1.   Connect the 3.3-V pin to the 3.3-V power node from the step-down voltage regulator.

2.   Connect the GND pin on the 3.3-V side to the ground node for the Arduino.

3.   Connect the B0 pin on the 3.3-V side to Rx or RxD pin on the ESP-01. This corresponds to the A0 pin on the 5-V side.

4.   Connect the 5-V pin to the 5-V power pin on the Arduino.

5.   Connect the GND pin on the 5-V side to the ground node for the Arduino.

6.   Connect the A0 pin on the 5-V side to Tx3 pin on the Arduino. This pin corresponds to the B0 pin on the 3.3-V side.

DC Motor

1.   Connect the GND terminal to the OB pin on the L9110 DC motor driver chip.

2.   Connect the Vcc terminal to the OA pin on the L9110 DC motor driver chip.

3.   Attach the plastic fan to the shaft of the DC motor.

L9110 DC Motor Driver Chip

1.   Connect the OA pin to the positive terminal of the DC motor.

2.   Connect the OB pin to the negative terminal of the DC motor.

3.   Connect the Vcc pins to the 5-V output from the power supply module located on the breadboard.

4.   Connect the GND pins to the ground node for the Arduino.

5.   Connect the forward signal or IB pin to digital pin 7 on the Arduino.

6.   Connect the backward signal or IA pin to digital pin 8 on the Arduino.

YwRobot 545043 Power Supply Module for Breadboard

1.   Put the power supply module on the breadboard so that the power and ground pins line up with the power and ground columns on the breadboard.

2.   Make sure that the jumper selection for the output power pins you will be using are set to a 5-V output.

3.   Connect the 5-V output of the power supply module to the Vcc pins on the L9110 chip.

4.   Connect the GND on the power supply to the ground node for the Arduino.

See Figure 9-16.

Figure 9-16   The wireless remote controlled DC motor system.

Setting Up the Software

The IBForwardSignal variable holds the Arduino pin number that is connected to the IB forward signal pin on the L9110 DC motor driver chip. The default pin is set to digital pin 7.

int IBForwardSignal = 7;

The IABackwardSignal variable holds the Arduino pin that is connected to the IA or backward signal pin on the L9110 DC motor driver chip. The default value is digital pin 8 on the Arduino.

int IABackwardSignal = 8;

The InitializeDCMotor() Function

The InitializeDCMotor() function initializes the DC motor by setting the pins on the Arduino that control the spin direction of the motor to OUTPUT pins that will output voltages to the L9110 DC motor control chip. See Listing 9-5.

Modifying the setup() function

Elements related to the DC motor are initialized in the setup() function by calling the InitializeDCMotor() function.

Modifying the CheckAndProcessIPD() Function

The “forward” command spins the DC motor’s shaft in the forward direction by sending a HIGH voltage signal to the IB or forward signal pin on the L9110 DC motor control chip. A text string is also sent to the Android controller indicating that the DC motor has been activated to spin in the forward direction. See Listing 9-6.

The backward command activates the DC motor and spins the shaft in the backward direction by sending a HIGH voltage signal to the IA or backward signal pin on the L9110 DC motor chip. A text string is also sent to the Android controller indicating that the DC motor has been activated and is spinning in the backward direction. See Listing 9-7.

The stop command deactivates the DC motor by sending a LOW voltage signal to both the IB and IA pins located on the L9110 DC motor control chip. A text string is also sent to the Android controller indicating that the DC motor has been stopped. See Listing 9-8.

Operating the DC Motor Control System

Download and install the Arduino DC motor project for this chapter on your Arduino Mega 2560. Connect a power source to the external power supply module located on the breadboard and turn on the power supply module by pressing down the on/off switch located next to the DC barrel jack. You should see a green light activate on the power supply. Next, start up the Arduino’s Serial Monitor. You should see something like the following initialization sequence:

Connect your Android phone to the Wi-Fi access point located on the ESP-01 module. Start up the Android basic wireless framework application and connect to the TCP server running on the ESP-01 module. The Android should connect to the TCP server and change the name of the device. In my case the name of my Android device is set to “verizon”.

Issue a “forward” command to the Arduino using the Android application. The DC motor’s fan should start rotating and a text string is sent to the Android controller indicating that the motor is set to forward rotation.

Next, issue a “stop” command to the Arduino. This should stop the rotation of the fan and send a text string to the Android controller indicating that the DC motor has been stopped.

Next, issue a “backward” command to the Arduino. This starts the rotation of the fan in the backward direction and also sends a text string to the Android controller indicating that the DC motor is rotating in the backward direction. You should also feel a breeze coming from the rotating fan now.

Next, issue a "forward" command to the Arduino. You should see the fan blades slow down and change direction.

Figure 9-17 shows the DC motor with an attached fan, the L9110 DC motor driver chip, the external breadboard power supply module, and an external battery power source while the DC motor is active and is turning the fan.

Figure 9-17   The DC motor with fan setup.

A front view of the rotating fan is shown in Figure 9-18.

Figure 9-18   Front view of an active DC motor with attached fan.

The Stepper Motor and the ULN2003 Stepper Motor Driver Board

The stepper motor is designed to provide a full 360 degree continuous rotation ability (in the form of a shaft that moves in small discrete steps) combined with the strength to hold the shaft’s position. The 28byj-48 stepper motor is a common inexpensive stepper motor that is found on websites such as Amazon. See Figure 9-19.

Figure 9-19   The 28byj-48 stepper motor.

In order to use this stepper motor with the Arduino we need the ULN2003 stepper motor driver board. See Figure 9-20.

Figure 9-20   The ULN2003 stepper motor driver board.

Hands-on Example: The Wireless Remote Controlled Stepper Motor System Using the Arduino Mega 2560 and the ESP-01 Server

In this hands-on example I show you how to build and operate a wireless remote control system for a stepper motor using an Arduino Mega 2560 and an ESP-01 module that functions as a server.

Parts List

   1 Android cell phone

   1 Arduino Mega 2560 microcontroller

   1 5- to 3.3-V step-down voltage regulator

   1 Logic level converter from 5 to 3.3 V

   1 ESP-01 Wi-Fi module

   1 Arduino development station such as a desktop or a notebook

   1 Breadboard

   1 28byj-48 stepper motor

   1 ULN2003 stepper motor driver board

   1 Power supply module for a breadboard (such as the YwRobot 545043 power supply module)

   1 Package of wires to connect the components

Setting Up the Hardware

Step-Down Voltage Regulator Connections

1.   Connect the Vin pin on the voltage regulator to the 5-V power pin on the Arduino Mega 2560.

2.   Connect the Vout pin on the voltage regulator to a node on your breadboard represented by a horizontal row of empty slots. This is the 3.3-V power node that will supply the 3.3-V power to the ESP-01 module as well as provide the reference voltage for the logic level shifter.

3.   Connect the GND pin on the voltage regulator to the GND pin on the Arduino or form a GND node similar to the 3.3-V power node in the previous step.

ESP-01 Wi-Fi Module Connections

1.   Connect the Tx or TxD pin to the Rx3 pin on the Arduino Mega 2560. This is the receive pin for the serial hardware port 3.

2.   Connect the EN or CH_PD pin to the 3.3-V power node from the step-down voltage regulator.

3.   Connect the IO16 or RST pin to the 3.3-V power node from the step-down voltage regulator.

4.   Connect the 3V3 or VCC pin to the 3.3-V power node from the step-down voltage regulator.

5.   Connect the GND pin to the ground pin or node.

6.   Connect the Rx or RxD pin to pin B0 on the 3.3-V side of the logic level shifter. The voltage from this pin will be shifted from 5 V from the Arduino to 3.3 V for the ESP-01 module.

Logic Level Shifter

1.   Connect the 3.3-V pin to the 3.3-V power node from the step-down voltage regulator.

2.   Connect the GND pin on the 3.3-V side to the ground node for the Arduino.

3.   Connect the B0 pin on the 3.3-V side to Rx or RxD pin on the ESP-01. This corresponds to the A0 pin on the 5-V side.

4.   Connect the 5-V pin to the 5-V power pin on the Arduino.

5.   Connect the GND pin on the 5-V side to the ground node for the Arduino.

6.   Connect the A0 pin on the 5-V side to Tx3 pin on the Arduino. This pin corresponds to the B0 pin on the 3.3-V side.

28byj-48 Stepper Motor

1.   Connect the cable from the stepper motor to the ULN2003 stepper motor driver board.

ULN2003 Stepper Motor Driver Board

1.   Connect the negative or GND pin to the Arduino’s common ground node.

2.   Connect the positive or Vcc pin to the external power supply’s 5-V pin.

3.   Connect the board to the cable from the stepper motor.

4.   Connect pin IN1 to digital pin 7 on the Arduino.

5.   Connect pin IN2 to digital pin 8 on the Arduino.

6.   Connect pin IN3 to digital pin 9 on the Arduino.

7.   Connect pin IN4 to digital pin 10 on the Arduino.

YwRobot 545043 Power Supply Module for Breadboard

1.   Put the power supply module on the breadboard so that the power and ground pins line up with the power and ground columns on the breadboard.

2.   Make sure that the jumper selection for the output power pins you will be using is set to a 5-V output.

3.   Connect the 5-V output of the power supply module to the + or Vcc pin on the ULN2003 board.

4.   Connect the GND on the power supply to the ground node for the Arduino.

See Figure 9-21.

Figure 9-21   The wireless remote controlled stepper motor control system.

Setting Up the Software

This section covers the new code specifically related to the stepper motor. The Arduino has a built-in stepper motor library that is included with the standard Arduino IDE. The library is called “Stepper.h” and must be included in the Arduino sketch.

#include <Stepper.h>

The MotorPinX variables define the Arduino pins that will be connected to the ULN2003 board. The pins by default range from digital pin 7 through digital pin 10.

The StepsPerRevolution variable holds the number of steps that the stepper motor’s shaft must take in order for 1 revolution or 360 degrees to occur. For 28byj-48 stepper motor the number of steps it takes for 1 revolution of the shaft is 2038.

const int StepsPerRevolution = 2038;

The AngleToSteps variable holds the conversion value that is used to convert an angle in degrees to the corresponding number of steps the motor must take in order to move the shaft that angle amount in degrees.

const float AngleToSteps = 2038/360.0f;

The Speed variable holds the speed in rotations per minute that the stepper motor will move. The default value is set to 6.

int Speed = 6;

The StepperMotor1 variable represents the stepper motor and is initialized by setting the steps the motor needs to take for 1 complete rotation, and the Arduino pins that are connected to the ULN2003 motor driver board.

The InitializeStepperMotor() Function

The InitializeStepperMotor() function initializes the stepper motor by setting the stepper motor’s default rotations per minute and setting the Arduino pins that are connected to the ULN2003 motor driver board to be OUTPUT pins that can provide output voltage signals to the board. See Listing 9-9.

Modifying the setup() Function

The stepper motor is initialized in the setup() function by calling the InitializeStepperMotor() function.

Modifying the CheckAndProcessIPD (String line) Function

The angledelta command directs the stepper motor to rotate the shaft by doing the following:

1.   The rotation angle is found by parsing the value after the equals sign and converting this string into an integer value.

2.   The rotation angle is converted to the number of steps the motor must execute in order to achieve this angle.

3.   The number of steps is then printed out to the Arduino Serial Monitor.

4.   The stepper motor moves the shaft the required number of steps by calling the step() function.

5.   A text string is sent back to the Android controller indicating that the stepper motor has finished moving the shaft the rotation angle amount specified by the user.

See Listing 9-10.

The speed command changes the rotations per minute of the stepper motor’s shaft by doing the following:

1.   The command is parsed and the speed which is located after the equals sign is converted from a string to an integer.

2.   The speed of the stepper motor is then set by calling the setSpeed(Speed) function with the new speed as the parameter.

3.   A text string is sent back to the Android controller indicating that the speed has been set for the stepper motor.

See Listing 9-11.

Operating the Wireless Remotely Controlled Stepper Motor System

Download and install the Arduino stepper motor project for this chapter on your Arduino Mega 2560. Connect a power source to the external power supply module located on the breadboard and turn on the power supply module by pressing down the on/off switch located next to the DC barrel jack. You should see a green light activate on the power supply. Next, start up the Arduino’s Serial Monitor. You should see something like the following initialization sequence:

Connect your Android phone to the Wi-Fi access point located on the ESP-01 module. Start up the Android basic wireless framework application and connect to the TCP server running on the ESP-01 module. The Android should connect to the TCP server and change the name of the device. In my case the name of my Android device is set to “verizon.

Next, send an “angledelta=90” command to the Arduino from the Android application. This should turn the stepper motor’s shaft by 90 degrees. After the stepper motor finishes the angle change a text string is sent back to the Android controller indicating that the rotation has been completed.

Next, send an “angledelta=-90” command to the Arduino. This should move the stepper motor’s shaft 90 degrees in the opposite direction. Note that the number of steps should be –509 where earlier the number of steps was 509. A text string acknowledging the rotation is sent back to the Android controller once the rotation has been completed.

Next, issue the command “angledelta=360” to move the shaft in 1 full rotation. Note the speed of the rotation.

Next, send a “speed=12” command to the Arduino to increase the speed of the rotations to 12 rotations per minute from the default 6 rotations per minute.

Next, send an "angledelta=360" command to the Arduino to make the shaft rotate 1 full rotation. The shaft should be rotating about twice as fast as before.

The ArduCAM Library and GitHub

For this book I used version 3.4.7 of the ArduCAM library that was released on 8/8/2015 for most of the hands-on examples involving the ArduCAM Mini OV2640 and the ArduCAM Mini OV2640 Plus cameras. For the following hands-on example that involves the ESP8266 processor on the NodeMCU I used the most recent version of the ArduCAM library that was available which was version 4.1.2 released on 10/15/2018. The reason was that this updated library contained the needed code to support the ESP8266 processor.

ArduCAM has a software repository on GitHub where you can download the current and previous versions of the ArduCAM library. If you are having trouble getting the camera examples to work with the most current version of the ArduCAM library then try installing the exact versions that I used for each of the hands-on examples.

   Main Repository: https://github.com/ArduCAM

   Arduino Repository: https://github.com/ArduCAM/Arduino

Hands-on Example: The Wireless NodeMCU and ArduCAM Mini OV2640 Plus Surveillance System

In this hands-on example I show you how to build and operate a wireless remote-controlled video surveillance system using a NodeMCU and ArduCAM Mini OV2640 Plus digital camera.

Parts List

   1 Android cell phone

   1 NodeMCU microcontroller

   1– ArduCAM Mini OV2640 Plus digital camera

   1 Breadboard

   1 Package of wires to connect the components

Note: Installation of a version of the ArduCAM library which supports the ESP8266 processor such as ArduCAM library version 4.1.2 that was released on 10/15/2018 is required.

Setting Up the Hardware

ArduCAM Mini OV2640 Plus

1.   Connect the SDA pin on the camera to pin D2 on the NodeMCU.

2.   Connect the SCL pin on the camera to pin D1 on the NodeMCU.

3.   Connect the MOSI pin on the camera to pin D7 on the NodeMCU.

4.   Connect the MISO pin on the camera to pin D6 on the NodeMCU.

5.   Connect the SCLK pin on the camera to pin D5 on the NodeMCU.

6.   Connect the CS pin on the camera to pin D0 on the NodeMCU.

7.   Connect the VCC pin on the camera to a 3.3-V power pin on the NodeMCU.

8.   Connect the GND pin on the camera to a GND pin on the NodeMCU.

See Figure 9-22.

Figure 9-22   The wireless remote-controlled NodeMCU and ArduCAM Mini OV2640 Plus surveillance system.

Setting Up the Software

The Arduino program for this hands-on example is based on the NodeMCU basic framework version 2.0 of the server presented previously in Chapter 8. In this section I will cover only the new code that is specific to this hands-on example using the ArduCAM Mini with the NodeMCU.

The SPI_CS variable which holds the pin on the NodeMCU that serves as the chip select pin and is used to select if the ArduCAM is processing data coming from the SPI bus. The default value is set to pin D0 on the NodeMCU.

const int SPI_CS = D0;

Modifying the InitializeCamera() Function

The InitializeCamera() function is modified by adding code that initializes the I2C interface by calling the Wire.begin() command if the processor being compiled for is the ESP8266.

The SendBinaryDataToClient() Function

The SendBinaryDataToClient() function sends binary data to a client by doing the following:

1.   Searching through the Clients array and attempting to find the name of the target client.

2.   If the client is found then send the binary data to the client by calling the write() function with a pointer to the beginning of the array of data and the length of the data as parameters.

3.   If the target client is found then return a 1 value otherwise return a 0 value.

See Listing 9-12.

Modifying the ReadFifoBurst() Function

The ReadFifoBurst() function was modified from a version used with the Arduino Mega 2560 and ESP-01 Wi-Fi module. The change is that the SendTCPBinaryData() function is replaced by the SendBinaryDataToClient() function to send binary data. See Listing 9-13. The changes are shown in bold print.

Modifying the TransmitImageSize() Function

The TransmitImageSize() function is modified by replacing the SendTCPMessage() function by the SendDataToClient() function in order to send data to clients. The modified code is in bold print. See Listing 9-14.

Modifying the setup() Function

The setup() function is modified by calling the InitializeCamera() function in order to initialize the ArduCAM Mini digital camera.

Modifying the ProcessIncomingClientData() Function

The camera-related commands are added into the ProcessIncomingClientData() function. The commands are functionally the same as those in the camera surveillance program for the Arduino Mega and ESP-01 Wi-Fi module. See Listing 9-15.

Operating the Surveillance System

Download and install the surveillance program for the NodeMCU and ArduCAM Mini Plus for this chapter on your NodeMCU. Start up the Serial Monitor and you should see something like the following if you are using a Windows 10 system. If you are using an XP operating system you might not see any start-up text.

Connect your Android device to the Wi-Fi access point on the NodeMCU. Start up the basic wireless framework application and connect to the TCP server running on the NodeMCU. The name of the android device should be changed and a “Client ID Received” text string should be sent back to the Android controller.

Next, take a photo using the camera at qvga resolution by sending a "qvga" command to the NodeMCU. You should see something like the following on the Serial Monitor. The photo should be transferred to and displayed on your Android device.

Next, take a photo using the qqvga resolution by sending a “qqvga” command to the NodeMCU. You should see something like the following on the Serial Monitor:

Next, take a picture in vga resolution by sending a “vga” command to the NodeMCU. The picture may be very dark at first so issue several vga commands to get a normal lightened picture.

An example of a screen capture from the Android controller displaying a photo inside the basic wireless framework version 2.0 application is shown in Figure 9-23.

Figure 9-23   Android screen capture of a VGA photo from the NodeMCU and ArduCAM Mini Plus camera.

If you select the store photo option from the Android basic wireless framework menu then incoming images will be saved to the Android’s storage space under the Pictures/CAMPics directory. An example of such a picture is shown in Figure 9-24.

Figure 9-24   A saved VGA photo in 640 by 480 pixel resolution.

The MQ-2 Smoke Detector

The MQ-2 sensor is capable of detecting smoke as well as other gases such as methane and propane. The specifications for the sensor are:

   Operating voltage: 5 V

   Operating current: 160 mA

   Operating temperature: −20 through 50 degrees Centigrade

   Power consumption: Less than 800 mW

   Preheat time: Over 24 hours

The easiest way to use the MQ-2 sensor is to buy one that has already been integrated into a module such as that shown in Figure 9-25.

Figure 9-25   The MQ-2 smoke detector.

The 5-V Active Buzzer

The active buzzer operates on 5 V and produces a sound when 5 V is applied to the positive terminal of the buzzer. See Figure 9-26.

Figure 9-26   The 5-V active buzzer.

Hands-on Example: The Wireless Remote Controlled Smoke Detector Alarm System Using the Arduino Mega 2560 and the ESP-01 Server

In this hands-on example I show you how to build and operate a wireless remote controlled smoke detector alarm system using an Arduino Mega 2560 and an ESP-01 Wi-Fi module configured as a server.

Parts List

   1 Android cell phone

   1 Arduino Mega 2560 microcontroller

   1 5- to 3.3-V step-down voltage regulator

   1 Logic level converter from 5 to 3.3 V

   1 ESP-01 Wi-Fi module

   1 Arduino development station such as a desktop or a notebook

   1 Breadboard

   1 MQ-2 smoke detector

   1 5-V active buzzer

   1 Power supply module for a breadboard (such as the YwRobot 545043 power supply module)

   1 Package of wires to connect the components

Setting Up the Hardware

Step-Down Voltage Regulator Connections

1.   Connect the Vin pin on the voltage regulator to the 5-V power pin on the Arduino Mega 2560.

2.   Connect the Vout pin on the voltage regulator to a node on your breadboard represented by a horizontal row of empty slots. This is the 3.3-V power node that will supply the 3.3-V power to the ESP-01 module as well as provide the reference voltage for the logic level shifter.

3.   Connect the GND pin on the voltage regulator to the GND pin on the Arduino or form a GND node similar to the 3.3-V power node in the previous step.

ESP-01 Wi-Fi Module Connections

1.   Connect the Tx or TxD pin to the Rx3 pin on the Arduino Mega 2560. This is the receive pin for the serial hardware port 3.

2.   Connect the EN or CH_PD pin to the 3.3-V power node from the step-down voltage regulator.

3.   Connect the IO16 or RST pin to the 3.3-V power node from the step-down voltage regulator.

4.   Connect the 3V3 or VCC pin to the 3.3-V power node from the step-down voltage regulator.

5.   Connect the GND pin to the ground pin or node.

6.   Connect the Rx or RxD pin to pin B0 on the 3.3-V side of the logic level shifter. The voltage from this pin will be shifted from 5 V from the Arduino to 3.3 V for the ESP-01 module.

Logic Level Shifter

1.   Connect the 3.3-V pin to the 3.3-V power node from the step-down voltage regulator.

2.   Connect the GND pin on the 3.3-V side to the ground node for the Arduino.

3.   Connect the B0 pin on the 3.3-V side to Rx or RxD pin on the ESP-01. This corresponds to the A0 pin on the 5-V side.

4.   Connect the 5-V pin to the 5-V power pin on the Arduino.

5.   Connect the GND pin on the 5-V side to the ground node for the Arduino.

6.   Connect the A0 pin on the 5-V side to Tx3 pin on the Arduino. This pin corresponds to the B0 pin on the 3.3-V side.

MQ-2 Smoke Sensor

1.   Connect the GND pin to the GND node for the Arduino.

2.   Connect the VCC pin to the 5-V output on the power supply module on the breadboard.

3.   Connect the OUT pin to digital pin 7 on the Arduino.

The 5V Active Buzzer

1.   Connect the GND pin to the GND node for the Arduino.

2.   Connect the positive terminal to digital pin 8 on the Arduino.

YwRobot 545043 Power Supply Module for Breadboard

1.   Put the power supply module on the breadboard so that the power and ground pins line up with the power and ground columns on the breadboard.

2.   Make sure that the jumper selection for the output power pins you will be using are set to a 5-V output.

3.   Connect the 5-V output of the power supply module to the Vcc pin on the smoke detector.

4.   Connect the GND on the power supply to the ground node for the Arduino.

See Figure 9-27.

Figure 9-27   The wireless remote controlled smoke detector alarm system.

Setting Up the Software

The section discusses the new code that is specific to the smoke detector alarm system example.

The BuzzerPin holds the Arduino digital pin number that is connected to the positive terminal of the active buzzer and is set by default to pin 8.

int BuzzerPin = 8;

The SensorPin variable holds the Arduino digital pin that is connected to the smoke detector sensor’s output pin and is set by default to pin 7.

int SensorPin = 7;

Modifying the ProcessSensor() Function

The ProcessSensor() function is modified from previous versions by setting the Arduino pin that is connected to the active buzzer to a HIGH voltage value when the smoke detector detects smoke. This activates the buzzer and it produces a loud sound to indicate that the alarm has been tripped. The code changes are highlighted in bold print. See Listing 9-16.

Modifying the InitializeSensor() Function

The InitializeSensor() function is modified by adding code to set the Arduino pin connected to the buzzer to be an output pin. The modified code is highlighted in bold print. See Listing 9-17.

Modifying the CheckAndProcessIPD() Function

The testbuzzer command is added in to the CheckAndProcessIPD() function. This command activates the buzzer for 3 seconds and then sends a text string back to the Android controller indicating that the buzzer has been tested. You should hear a loud tone generated from the buzzer if the test was successful. See Listing 9-18.

Operating the Smoke Detector Alarm System

Download and install the smoke detector alarm example for this chapter on your Arduino. Activate the Serial Monitor and you should see something similar to the following:

Connect your Android cell phone to the Wi-Fi access point on the ESP-01 module. Start up the Android basic framework application version 2.0 and connect to the TCP server running on the ESP-01. Once connected, the name of Android device on the network is changed. In my case the name is changed to verizon.

Next, test the active buzzer by issuing a “testbuzzer” command from the Android application. You should hear the buzzer generate a sound and then receive a text string back to your Android indicating that the buzzer has been tested.

Next, turn off the call out and the text message features on the Android application. Then, activate the alarm system on the Arduino from the Android application. After the 10-second wait period the smoke alarm is fully activated.

Next, the server is by default the target device so select “Monitor Target Device” from the basic wireless framework’s main menu to set the server as one of the devices that will be monitored. Then activate the network monitoring to begin reading in the number of hits that the alarm system has generated. The number of hits should be 0 now if there is no smoke in the room. Note: The smoke detector might need a few minutes to warm up so you may get some false positives for a few minutes or other erratic behavior.

Next, use something that will generate smoke such as lighted incense stick to trigger the smoke detector. I used an incense stick and brought the smoke just under the smoke detector. This should trigger the sensor. When the Android sends the next "hits" command the Arduino will send an updated value of 1. The Android will then reset the number of hits back to zero on the Arduino by sending a "reset" command to the Arduino.

Next, pull the smoke source away from the sensor and put it out. Deactivate the alarm using the Android application.

The MQ-2 Smoke Detector (Analog Version)

The MQ-2 smoke detector can produce analog output as well as digital output. The MQ-2 module shown in Figure 9-28 has both digital and analog outputs.

Figure 9-28   The MQ-2 smoke detector (with analog output).

For this model of MQ-2 sensor there is a power LED to indicate that the sensor is on and a DOUT LED to indicate if the digital output senses smoke. The analog pin on the module is labeled A0. See Figure 9-29.

Figure 9-29   The back of the MQ-2 smoke detector (with analog output).

Hands-on Example: Wireless Remote Controlled Smoke Detector (Analog) Alarm System Using Arduino and ESP-01

In this hands-on example I show you how to build and operate a wireless remote controlled smoke detector (analog output) alarm system using an Arduino Mega 2560 and an ESP-01 Wi-Fi module configured as a server. The key difference between this example and the last one is that the MQ-2 sensor has an analog output that gives you more control over the amount of smoke that triggers the alarm.

Parts List

   1 Android cell phone

   1 Arduino Mega 2560 microcontroller

   1 5- to 3.3-V step-down voltage regulator

   1 Logic level converter from 5 to 3.3 V

   1 ESP-01 Wi-Fi module

   1 Arduino development station such as a desktop or a notebook

   1 Breadboard

   1 MQ-2 smoke detector module (with analog output)

   1 5-V active buzzer

   1 Power supply module for a breadboard (such as the YwRobot 545043 power supply module)

   1 Package of wires to connect the components

Setting Up the Hardware

Step-Down Voltage Regulator Connections

1.   Connect the Vin pin on the voltage regulator to the 5-V power pin on the Arduino Mega 2560.

2.   Connect the Vout pin on the voltage regulator to a node on your breadboard represented by a horizontal row of empty slots. This is the 3.3-V power node that will supply the 3.3-V power to the ESP-01 module as well as provide the reference voltage for the logic level shifter.

3.   Connect the GND pin on the voltage regulator to the GND pin on the Arduino or form a GND node similar to the 3.3-V power node in the previous step.

ESP-01 Wi-Fi Module Connections

1.   Connect the Tx or TxD pin to the Rx3 pin on the Arduino Mega 2560. This is the receive pin for the serial hardware port 3.

2.   Connect the EN or CH_PD pin to the 3.3-V power node from the step-down voltage regulator.

3.   Connect the IO16 or RST pin to the 3.3-V power node from the step-down voltage regulator.

4.   Connect the 3V3 or VCC pin to the 3.3-V power node from the step-down voltage regulator.

5.   Connect the GND pin to the ground pin or node.

6.   Connect the Rx or RxD pin to pin B0 on the 3.3-V side of the logic level shifter. The voltage from this pin will be shifted from 5 V from the Arduino to 3.3 V for the ESP-01 module.

Logic Level Shifter

1.   Connect the 3.3-V pin to the 3.3-V power node from the step-down voltage regulator.

2.   Connect the GND pin on the 3.3-V side to the ground node for the Arduino.

3.   Connect the B0 pin on the 3.3-V side to Rx or RxD pin on the ESP-01. This corresponds to the A0 pin on the 5-V side.

4.   Connect the 5-V pin to the 5-V power pin on the Arduino.

5.   Connect the GND pin on the 5-V side to the ground node for the Arduino.

6.   Connect the A0 pin on the 5-V side to Tx3 pin on the Arduino. This pin corresponds to the B0 pin on the 3.3-V side.

MQ-2 Smoke Sensor

1.   Connect the GND pin to the GND node for the Arduino.

2.   Connect the VCC pin to the 5-V output on the power supply module on the breadboard.

3.   Connect the AO pin to analog pin A12 on the Arduino.

The 5-V Active Buzzer

1.   Connect the GND pin to the GND node for the Arduino.

2.   Connect the positive terminal to digital pin 8 on the Arduino.

YwRobot 545043 Power Supply Module for Breadboard

1.   Put the power supply module on the breadboard so that the power and ground pins line up with the power and ground columns on the breadboard.

2.   Make sure that the jumper selection for the output power pins you will be using are set to a 5-V output.

3.   Connect the 5-V output of the power supply module to the Vcc pin on the smoke detector.

4.   Connect the GND on the power supply to the ground node for the Arduino.

See Figure 9-30.

Figure 9-30   The wireless remote controlled analog smoke alarm system.

Setting Up the Software

This section will cover only the changes in code from the previous hands-on example of the digital smoke sensor.

The SensorPin variable holds the Arduino pin that is connected to the smoke detector’s analog output pin and is set by default to analog pin A12.

int SensorPin = A12;

The SensorTripLevel variable holds the level at which a reading from the analog smoke sensor will trip the alarm. The default value is set to 300. A lower value increases the sensitivity of the smoke detector.

int SensorTripLevel = 300;

The TestSensor variable is true if the smoke sensor is in test mode which will continuously read in the value from the smoke sensor and output it to the Serial Monitor. The default value is false or off.

boolean TestSensor = false;

The MinDelayBetweenTests variable holds the minimum time between reads that the smoke sensor will be read in test mode. The default value is 1 second or 1000 milliseconds.

unsigned long MinDelayBetweenTests = 1000UL;

The MinDelayBetweenTestsStartTime variable holds the time that the last read from the smoke sensor occurred while in test mode.

The ProcessSensorTest() Function

The ProcessSensorTest() function processes the sensor test function by doing the following:

1.   If the sensor is in test mode then continue the execution of the function otherwise exit the function.

2.   Calculate the elapsed time since the last test reading from the sensor.

3.   If the elapsed time is greater than or equal to the minimum time between tests then read the smoke sensor, print out the result to the Serial Monitor and then set the MinDelayBetweenTestsStartTime variable to the current time.

See Listing 9-19.

The ProcessSensor() Function

The ProcessSensor() function is very similar to the ProcessSensor() function discussed in previous examples throughout the book. However, there are a few changes that are made which are:

1.   The analogRead() function is used to read the value from the sensor’s analog output pin.

2.   If the value read in from the sensor is greater than or equal to the SensorTripLevel value then the amount of smoke in the room is enough to trip the alarm.

3.   If the alarm is tripped then the buzzer is activated by writing a HIGH voltage value to positive terminal of the buzzer.

The code modifications are highlighted in bold print. See Listing 9-20.

The InitializeSensor() Function

The InitializeSensor() function is modified from the previous version by printing out the sensor trip level to the Serial Monitor. Code modifications are highlighted in bold print. See Listing 9-21.

Modifying the CheckAndProcessIPD() Function

The triplevel command sets the level of smoke that will trigger the alarm and returns a text string to the Android controller indicating the new trip level. See Listing 9-22.

The testsensoron command activates the test mode for the sensor. See Listing 9-23.

The testsensoroff command deactivates the test mode for the sensor. See Listing 9-24.

The loop() Function

The loop() function is modified to support the testing mode of the analog smoke alarm by calling the ProcessSensorTest() function. The modified code is shown in bold print. See Listing 9-25.

Operating the Smoke Alarm

Download and install the analog smoke alarm project on your Arduino. Start up the Serial Monitor and you should see something like the following initialization sequence:

Connect your Android cell phone to the Wi-Fi access point on the ESP-01 module. Start up the Android basic framework application version 2.0 and connect to the TCP server running on the ESP-01. Once connected the name of Android device on the network is changed. In my case the name is changed to “lg”.

Next, test the active buzzer by issuing a “testbuzzer” command from the Android application. You should hear the buzzer produce a tone for a few seconds and then stop.

Next, let’s read and display the values coming from the smoke sensor by issuing a "testsensoron" command using the Android application. The specification for this sensor recommends a 24-hour burn-in period before the best results can be achieved but the sensor should be able to be useable after a few minutes of being turned on. If you are using the sensor for the first time then you should see the results of this test as rapidly decreasing until it becomes somewhat stable. For example, after a few minutes of being turned on for the first time my results eventually settled around 238 after decreasing from about 400.

Next, stop the sensor test by issuing a “testsensoroff” command from the Android application.

Next, turn off the call out and the text message features on the Android application. Then, activate the alarm system on the Arduino from the Android application. After the 10-second wait period the smoke alarm is fully activated.

On the Android application the server is by default the target device so select “Monitor Target Device” from the basic wireless framework’s main menu to set the server as one of the devices that will be monitored. Then activate the network monitoring to begin reading in the number of hits that the alarm system has generated. The number of hits should be 0 now if there is no smoke in the room. Note: The smoke detector might need a few minutes to warm up so you may get some false positives for a few minutes or other erratic behavior.

Next, create a smoke source such as a lighted incense stick and bring it near the smoke detector. The alarm should eventually trip and the next time the Android polls for the number of hits the value should be greater than 0. In my case the smoke produced a sensor value of 300 which tripped the alarm. A sound effect should then play on the Android device indicating that smoke has been detected. A “reset” command is then issued by the Android application to reset the number of hits back to 0.

Next, stop the network monitoring using the Android application. Then, deactivate the alarm.

Summary

The main focus of this chapter was on Arduino projects that required a separate power source for the hardware being controlled. These projects involved a servo, a DC motor with a fan attachment, a stepper motor, a digital smoke sensor, and an analog smoke sensor. An emergency backup battery power system was also presented. In addition, a video surveillance project involving an ArduCAM mini plus camera and a NodeMCU was given. For the downloadable content for this chapter I included the APK files for all the Android basic wireless framework projects. I also included the Android Studio project for the final version of the Android basic wireless framework using a more recent version of Android Studio. I also presented some troubleshooting tips for those having problems getting the examples to work correctly.