Thursday, December 13, 2018

ESP8266 / DS18B20 Temperature; Ajax Web Updates

I wanted to monitor temperature with a ESP8266, and display the results on a web page. I grabbed a DS18B20 temperature sensor, and created a web server to display the results. I decided to use Ajax, so that the results would autoupdate without having to reload the page. The question came up, what IP address did the ESP8266 grab, so I know where to point my browser. I had a I2C LCD, so I connected that to show the IP address. I probably should have assigned a static IP so that it never changes, but hey, now you will know how to use a LCD with a ESP8266.

I have the I2C LCD connected to the ESP8266 I2C pins, GPIO5 (SCL), and GPIO4 (SDA). The LoLin calls these D1 & D2 respectively. You can specify other pins in the Wire.begin() statement, but the library I'm using doesn't expose that command. My LCD does not run on 3.3v, so I have it connected to the USB Power pin, labeled VU on the LoLin.

The DS18B20 I connected to GPIO14 (D5), GND, and 3.3v, with a 4.7k Ohm resistor across the D5 and 3.3v.

When I put the IP address displayed on the LCD (or in serial monitor if you are not using a LCD) in my browser, I get the following display:

My next modification will be to email me when the temperature climbs above or drops below my preset "acceptable range", indicating a problem with my HVAC system.

OneWire and DallasTemperature found in Library Manager

Wednesday, December 5, 2018

Build a $200 Computer for Christmas

Do you want to learn Computers, Electronics, and Programming?

For the last 40+ years kids and adults alike have been building their own computers instead of buying prebuilt, preloaded systems. Times have changed, and so has hardware and software. Some of the coolest tech comes in kit form, so not a lot of building is required, but you can still put it together, and customize it the way you want.

For the budding programmer or electronics engineer, one of the least expensive ways to put together a full blown linux and IOT programming environment is with the Raspberry Pi. The new 3B+ is more powerful than previous versions of the $35 computer, and with onboard WiFi and Bluetooth, very flexible.

You can build a media server, a VPN device, firewall / router, security system, phone system, automated home control, and much more. Learn to program in Python, or even C/C++. Build web based apps with PHP and MySQL. The I/O pins allow you to read sensors and control relays and LED's. Your imagination is the limit! See Build a Home Automation System!

Let's see if we can build a complete computer system for $200 or less!

Raspberry Pi 3B+
The heart of the system, this single board computer includes the processor, ram, drive (sd card), video and audio, ethernet, WiFi and Bluetooth. A complete starter kit with power supply, case, sd card, HDMI cable, etc.
Costs about $85

Raspberry Pi 3 B+ (B Plus) with 1.4 GHz 64-bit Quad-Core Processor
Dual Band 2.4GHz and 5GHz IEEE 802.11.b/g/n/ac Wireless LAN
10/100 Ethernet
32 GB Samsung EVO+ Micro SD Card (Class 10) Pre-loaded with NOOBS
USB MicroSD Card Reader
CanaKit 2.5A USB Power Supply with Micro USB Cable and Noise Filter
Premium Raspberry Pi 3 B+ Case and more

Wireless Keyboard / Mouse Combo
This wireless keyboard and mouse combo makes it easy to use your new Raspberry Pi without wire clutter, and allows some distance between the units.
Costs about $40

Comfortable palmrest
Single usb wireless receiver
Over 2 year battery life

HDMI Monitor
You can use your existing HDMI enabled TV, or you can get this 20" LED HDMI monitor with built in speakers. This monitor gets the audio over the HDMI cable, so no external speaker / wire clutter. VESA wall mount ready. 2x HDMI ports, so another HDMI device can also be connected.
Costs about $70

Sensor Kit
For the electronics experimenter, add an optional project pack that includes 38 I/O gadgets, including temperature, relay, joystick, sound, and much more, with convenient solderless breadboard and Raspberry Pi I/O connector. Costs about $45

Tuesday, November 27, 2018

Python: Floating Point vs. Decimal

In our Arduino projects (programmed in C/C++), we often use a Raspberry Pi for collecting data, and the Python programming language.

Math with floating point is inherently risky, because a decimal number does not store exactly in binary.

in python,
1.1+2.2 = 3.3000000000000003

not the 3.3 you might expect.

we can force the result we are looking for by importing the Decimal module, and setting the desired precision (2 in this example):

>>>from decimal import *
>>> getcontext().prec = 2
>>> Decimal(1.1)+Decimal(2.2)

Arduino doesn't have a Decimal function, it uses floating point, but you can work around this by using Fixed Point Arithmetic

Friday, November 16, 2018

Why can't I upload to my Arduino Nano?

For all of you out there with Chinese NANO clones, or older Arduino NANO's, there has been a recent change. The upload baud rate has been bumped up from 57600 to 115200, and that causes a upload error.

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xf6
The solution is at hand!

Go to

Tools > Processor > Atmega328P (Old Bootloader)

and your upload woes should be solved!

Wednesday, September 26, 2018

Electrolysis Issues with Soil Moisture Sensor

One problem with testing for moisture in soil or for free water is electrolysis of the electrodes. As material is removed from one electrode and deposited on the other (a byproduct of using a dc current), the resistance changes, and the values coming from the sensor start to drift.

One way to solve this is to flip the polarity of the current each time you do a read. The following code will change the polarity of the pins (and the equation evaluation) each time through the loop.

This circuit will print the word "Triggered" if it senses moisture (even skin moisture) and light up the onboard LED. You could have additional sections of code for varying levels of moisture (very wet, wet, dry, very dry), based on the analog results.


Probe pin 1 - A0 //analog input
Probe pin 2 - A5 //digital output
2M ohm resistor between A0 (analog input) and A4 (digital output)


int state=0;
int aValue=0;

void setup() {
  // put your setup code here, to run once:
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);


void loop() {
  // put your main code here, to run repeatedly:

  if (state==0){
    digitalWrite(A4, HIGH);
    digitalWrite(A5, LOW);
    if (aValue<200){
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
  } else if (state==1){
    digitalWrite(A4, LOW);
    digitalWrite(A5, HIGH);
    if (aValue>800){
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);


Thursday, September 20, 2018

Long Distance Serial with RS-422

PC's used to have RS-232 ports to connect things like mice, modems, printers, etc. Then technology moved on to USB. RS-232 had a maximum distance of 50', and USB is 9' (USB 3.0) to 16' (USB 2.0). If you really need to send data a long distance, RS-422 might be the trick. It uses two pair of wires, and CAT 5 is just fine. You communicate up to 4900'. Arduino's and other microcontrollers use TTL logic (3v to 5v for a HIGH, 0v for a low), and need a converter to work with other signals. We are using a inexpensive TTL to RS-422 converter called a YL-128. This module uses a Maxim 490 chip.

By connecting a Arduino to a YL-128 on each end of the wire, you can communicate as if the two Arduino's were side by side and connected directly.

Connect one Arduino RX1 to the YL-128 RXD pin (RO), and the Arduino TX1 to the YL-128 TXD pin (DI). Connect a second Arduino and YL-128 on the other end of the cable in the same manner.

Connect the two YL-128 modules Y-A, Z-B, A-Y. and B-Z.

You can then load a serial test sketch like the one at

Friday, August 31, 2018

Driving Relays with the ESP8266

The ESP8266 has a current limit of 12ma, so we have to take care when driving relays.

First I start with a 817C Optoisolator. The datasheet says says the IR LED drops 1.2v, so I plug 3.3v source voltage, 1.2v forward voltage, and 12ma forward current into the calculator at and I get a 180 ohm resistor as the optimum value.

Now, according to the datasheet for the 817C, the Current transfer ratio at 12ma is around 120%, so I can only conduct around 14-15ma through the photo transistor, not nearly enough to trigger a relay.

Let's have the photo transistor trigger an additional transistor, one that can allow the full current needed by the coil of the relay. I have chosen a 2N5551, since the datasheet says it can handle 600ma and 5v.

Now the relay. I chose a HK19F 5v 125ohm relay which, according to Ohm's law and the datasheet, draws 40ma. It's a DPDT relay (most commercial modules use a SPDT), but the contacts are only rated for 1A or so.

Don't forget a flyback diode for that relay. I used a 1N4148, which, according to the datasheet, easily handles the current and voltage of the relay.

If you want, you could add a LED / 150 ohm resistor across the coil of the relay as a visual indicator.

Now a simple

digitalWrite(5, HIGH);

makes it all happen!

UPDATE: 3v MOSFET instead of optoisolator. Simpler and cheaper!

Thursday, August 30, 2018

Sync'ing ESP8266 GPIO Pins to Arduino Pin Numbers

Many ESP8266 12E Boards come with Arduino style pin numbers labeled on the boards, but you use the GPIO pin numbers in your code. If you include the following lines of code in your sketch before setup(), you can use the number silkscreened on your board (different boards may have different pin matchings).

const byte D0 = 16;
const byte D1 = 5;
const byte D2 = 4;
const byte D3 = 0;
const byte D4 = 2;
const byte D5 = 14;
const byte D6 = 12;
const byte D7 = 13;
const byte D8 = 15;
const byte D9 = 3;
const byte D10 = 1;

Now you can use something like

digitalWrite(D6, HIGH); //uses D6 on the ESP8266

in your sketch.

Of the above GPIO numbers, not all of them are easily available for use. Some are used for flash memory (GPIO 6 - 11), others are used for boot and flash purposes (GPIO 0, 2 & 15). GPIO 0-15 all have a built-in pull-up resistor (use INPUT_PULLUP), just like in an Arduino, while GPIO16 has a built-in pull-down resistor. The safe ones are GPIO 1, 3-5, 12-14, and 16. All GPIO pins can source about 12ma, and sink close to 20ma.

The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a value to a const variable (which is a good thing). Unlike #define, it types the "variable" instead of making the compiler guess. For these reasons it's preferable to use const instead of #define (

A byte stores an 8-bit unsigned number, from 0 to 255. This takes less space than an int, which takes up two bytes. You may see some code samples use uint8_t instead of byte. It means the same thing.

Further reading:

Thursday, July 5, 2018

ESP8266 Deep Sleep Mode

My Adafruit HUZZAH Feather ESP8266 consumes 0.080 amps (80 ma) in normal operation. With one wire and a single line of code, I can drop that to about 0.010 amps (10 ma), according to my USB power monitor.

I connected a wire from GPIO16 (pin 16) to RESET (RST).

I'm using the Arduino IDE to program my ESP8266, so at the end of my loop, I insert

void loop() {
    // other code in my sketch


Which puts the ESP8266 to sleep for 60 seconds, and then reboots, running through my code once more before going back to sleep. This greatly extends my battery life. You can change the 60 to whatever number of seconds you want the unit to sleep.

With a Adafruit TPL5110 Low Power Timer you can drop the power consumption of nearly any microcontroller or circuit to 20ua, for about $5.

Wednesday, June 27, 2018

ESP8266 Breakout Board

The bare ESP8266 chip is cheap, but has non standard pin spacing, and isn't easy to work with. There is a "Bread Board Friendly" breakout for it, but it has a couple of quirks.

1. It's wide enough that it takes the outer rows of pins on a standard solderless breadboard, so you need to connect your wires underneath, then plug the breakout board on top. I'll post a picture of this shortly.

2. Most descriptions for the board claim one of the included 10k ohm resistors (the right one) connect to GPIO2 (which would be wrong) when it's actually connected to GPIO15 (which is correct).

3. The middle "0" ohm resistor is actually a jumper, and needs to be removed if attaching a 3.3v regulator on the back side.

3. I recommend a external 3.3v supply of 600ma or more. As mentioned above, a 3.3v regulator can be attached to the back side, but ....

A number of resources claim the regulator is a LM31117-3.3v 800ma (and some adapter board dealers even include one). That part will not work, as pins 2 & 3 are reversed. You need a AP7215 (AP7215-33YG-13) 600ma 3.3v SOT-89 regulator. I do wish input and output capacitors (1uf is fine) would have been preinstalled on the board, or at least pads for them. Place one between pin 2 and gnd, and the other between pin 3 and gnd. Alternatively, you can use a external 3.3v regulator or module, as I am doing.

4. There are still a few more resistors you will have to add:
    GPIO0 to VCC with a 10k ohm
    RST to VCC with a 10k ohm
    RST to Gnd through a momentary (reset button)
    GPIO0 to Gnd through a momentary (program button) and a 470 ohm in series.

5. You will need a ttl serial programmer.

The good news is that it's very easy to solder the ESP8266 to the adapter if you have a fine point soldering iron.

Additional references:

Tuesday, June 26, 2018

Coulomb Counting and Amp Hours

Do you need to know how much Energy a device is consuming? How about producing? Maybe you want to keep track of how much juice is left in a battery pack? Now you can know!

Cliff Note version:
Take a Amp reading every second (Coulombs). Add those readings together (running total). Divide total by 3600 gives you Amp Hours. Multiply by the Volts to get Watt Hours.

Building battery "Fuel Gauges" is simple when you use Coulomb counting. A Coulomb is equal to 1A * 1s, so if your current sensor records a 5A draw this second, that's 5 Coulombs. If draw is steady for a minute, that would be 300 Coulombs (5 * 60), and if for an hour, 18000 Coulombs (5 * 60 * 60). 18000 / 60 / 60 = 5 Ah
If over two hours, you count 36000 Coulombs, 36000 / 60 / 60 = 10 Ah. So take the total Coulombs you have counted for whatever period of time period you choose, and divide by 3600 (60 * 60), that gives you your Ah charge (or discharge).

The following code simulates a current sensor reading as a fixed rate (0.5A). It prints the Coulomb totals every second, and the running Ah total.

Code Link

Stay tuned as we combine this with a current sensor and a voltage divider to get Amps, Volts, Ah, and Wh readings.

ACS715 Current Sensor Project

ACS712 Current Sensor Project

Current sensing with a shunt

Wednesday, June 20, 2018

The ESP8266 doesn't have EEPROM?

Unlike the Arduino AVR based controllers, the ESP8266 does not have a dedicated EEPROM. We can however use a bit of flash memory as a emulated EEPROM, and we can use the Arduino EEPROM library to do it. Using the Arduino Put and Get code samples as they are doesn't work. However a simple

EEPROM.begin(1024); // both the put and get code

before the

EEPROM.put(address, variable);
EEPROM.get(address, variable);

and a

EEPROM.commit(); // only on the put code

after it makes everything work.

In the linked code samples below, I'm saving 2 strings of text (wifi ssid and password) to the EEPROM in the put code, and retrieving them in the get code. If you put a slide switch in your project, and run the two pieces of code depending on the switch position, you can reset the credentials from a built in web form. I will cover that ability soon.



Fore more info, see:

APRS Transmitter

The HX1 is a low-power amateur radio transmitter set to a fixed frequency of 144.390 MHz. In North America, this frequency is used for the Automatic Packet Reporting System, or APRS

APRS is a standard used by amateur-radio operators to broadcast live sensor data on a shared frequency. This data can be received by anyone with the proper equipment, and is aggregated on the internet through gateways run by members of the APRS community. APRS is commonly used to share real-time data including vehicle location (GPS), weather stations, high-altitude balloon tracking, remote sensor platforms, disaster relief coordination, and more. It’s effectively an open-source, open-data, community-run, free to use, IoT system with potentially worldwide coverage.

The HX1 is a 5V, low-power (300mW), narrowband (3kHz) VHF FM transmitter module. With a simple antenna, the range will be around 6 miles. Since this is a “bare” transmitter, the data input must be properly modulated and formatted. There are several Arduino software packages that can do this, see the hookup guide for information.

Note: Because it uses amateur radio frequencies, you will need an amateur radio license to use the HX1. It’s easy (and fun!) to get one.

Tuesday, June 5, 2018

Arduino connected Micro Epsilon Laser Micrometer

With the success of our Igaging / Mitutoyo and Harbor Freight digital measuring devices projects, now it's time to turn our attention to the Micro-Epsilon Laser Micrometer, optoCONTROL 2600.

There are several types of outputs, including analog, digital RS-232 and RS-422, and Binary or ASCII options. We chose BINARY on the RS-422 output (19200, 8N1), and connected a RS-422 to TTL converter to Arduino 2560 (originally a Arduino Nano, but the 2560 has 4 hardware UARTs).

The program we selected is "EDGELH" "Standard edge dark-bright".

Wiring is as follows:

Micro Epsilon DB25 (Manual is wrong):
Pin 8 (T+) to RS-422 A/R+
Pin 7 (R+) to RS-422 Y/T+
Pin 20 (R-) to RS-422 Z/T-
Pin 21 (T-) to RS-422 B/R-


if you are using the blue YL-128
VCC to Arduino +5v
Gnd to Arduino Gnd
RSD to Arduino D19 (Rx1) // Serial1 Rx
TXD to Arduino D18 (Tx1) // Serial1 Tx

If you are using the orange 2578-TTL
VCC to Arduino +5v
Gnd to Arduino Gnd
TXD to Arduino D19 (Rx1) // Serial1 Rx
RXD to Arduino D18 (Tx1) // Serial1 Tx

I suggest a DB-25 M to RJ-45 F adapter, and make up (or cut down) a ethernet cable to connect to the screw terminals of the RS-422.


With no object in the beam, take your measurement, and subtract that in the code from the measured value. the "zero" function on the unit display only affects the display and analog output, the digital output shows the measurement from the bottom of the beam. I'll be adding a "zero" button and save that value to EEPROM.

Friday, May 18, 2018

RPM into MPH? Building a Anemometer!

I'm building an anemometer to measure wind speed. I'm building the typical 3 cup style anemometer, and using a hall effect sensor (digital, non-latching) to trigger an arduino (or esp8266).

It's a open collector unit (output pulls low when triggered with a magnet), and therefore uses a 10k ohm pull up resistor, or use

pinMode(inPin, INPUT_PULLUP);

val = digitalRead(inPin); // read the input pin

and watch for a LOW (FALLING when using interrupts -

Inside the unit, I want to pass a small magnet across the sensor. I could use 1 or more magnets, and adjust the code for the number of pulses (triggers) per revolution. Let's assume 1 magnet, so one pulse per revolution.

The math looks like this:

RPM = cycles per second (Hz) * 60 seconds per minute / # of magnets

(to count cycles per second or HZ -

FPM = Diameter (inches) / 12 * 𝝅 * RPM

FPH = FPM * 60

MPH = FPH / 5280

So 3" / 12 * 3.14 * 100 RPM * 60  / 5280 = 7.14 MPH

RPM = Revolutions Per Minute
FPM = Feet Per Minute
FPH = Feet Per Hour
MPH = Miles Per Hour
𝝅 = 3.14 (pi)
Diameter = inches from center of shaft to center of cup * 2

More on this to come.

Optical Tach

Hall Effect Tach

Ultrasonic speed and direction, no moving parts

Thursday, April 26, 2018

The Arduino 18650 Lithium Ion Battery Charger

The 18650 Lithium Ion Battery is becoming the go to battery for rechargeable projects, from microcontrollers to skateboards and ebikes.

We have been powering our Raspberry Pi, Arduino, and Ham Radio projects with them.

Although soldering the tabs on these batteries have been difficult (and damaging) in the past, there are now solderless interlocking kits that make them very easy to use.

It's extremely important to make sure these batteries are charged properly! Henry's Bench comes to the rescue with a Arduino controlled charger for these batteries. Just add a LCD screen to see voltage, amps, and cell temperatures.

Explanation of Lithium Ion charging profiles and dangers -

To learn more about building battery packs with the 18650, the master tome of all info is DIY Lithium Batteries (How to build your own battery packs), by Micah Toll (subscribe to his YouTube channel).

Tuesday, April 17, 2018

Adding a LCD to a ESP8266

Tutorials for adding a LCD to a Arduino are all over the net. It's pretty simple, and I2C makes the wiring even simpler, and libraries prevent complicating the code much. However, adding a LCD to a ESP8266 is not as common, and there is much misinformation posted about logic levels, and what not. So here is a working project using a I2C LCD, and a ESP8266. Wiring is very simple.

SCL to SCL (D1 or GPIO5 on a LoLin)
SDA to SDA (D2 or GPIO4 on a LoLin)
(these two pins are redirectable to another set in the Wire.begin command:

//Wire.begin(); default
Wire.begin(0, 2); // specify different SDA, SCL pins

Gnd to Gnd
+5v to +5v (USB or Vin)

Yes, the LCD prefers 5v, and it won't affect the 3.3v logic of the ESP8266, as the ESP8266 I2C is in output, not input mode.

Some slower I2C devices won't work out of the box with the ESP8266 because of timing issues, and a stretch adjustment needs to be made. We found one I2C display that would not register on the I2C scanner sketch, but the rest did. If the scanner finds it when connected to a UNO, but not a ESP8266, place the following command after the Wire.begin line.


Make sure you run the I2C scanner first, to check what I2C address your display uses. Ours uses 0x27. See more in our Instructable at

If you don't have one already installed, you will need to install a I2C LCD Library. Those instructions are included in the Instructable.

Saturday, March 31, 2018

Controlling a ESP8266 with Alexa

Here is an easy project using a Echo Dot (Alexa) to control my ESP8266. I give it simple commands (Alexa, lights, off) and the command shows on the LCD screen attached to the ESP8266. Actual relays will be the next step to attach.

The project is based on the Adafruit tutorial at

with LCD and other modifications made by me. My code is found at

Also see my ESP8266 LCD Project at

Friday, February 23, 2018

5mm LED Holders

One of the things I find myself making often, is plastic (plexiglass) control panels. I still have not made my desktop cnc yet, but when I do, I can stop drilling these panels by hand. Mounting LED's is always a task, and I started using these very nice chrome LED holders. They come with a plastic plug for the 2 LED leads to stick through, and hold the LED securely. For my 4 lead RGB LED's, I just use a spot of hot glue to hold the LED, and heat shrink tubing to insulate the leads. Use a silicone coated wire for flexibility and stress relief.

Tuesday, February 20, 2018

FreePBX - Open Source PBX Phone System

I just installed FreePBX, a linux based open source phone system at work (12 SIP trunks, 40 extensions), and thought, why not a home / small office unit based on a Raspberry Pi? The Raspberry Pi can easily handle multiple SIP trunks, and enough IP phones for the whole family / small business to have a extension or even a private DID. The FreePBX software is free, the Raspberry Pi is inexpensive (about $80 for a complete kit) and PolyCom IP Phones are readily available for around $80 each. Now you can have individual voicemail (email integrated), auto attendant, intercom, paging / bells, and music on hold just like the big boys. The phones and paging adapter can be powered by a inexpensive POE network switch, as can the Raspberry Pi with a POE Hat.

Arduino integration? Yes your Arduino can make outbound calls with prerecorded alerts!

Thursday, January 11, 2018

ATX Bench Power Supply

As a Ham Radio operator and a electronics / programming fanatic, I'm always on the lookout to improve my bench equipment. This weeks upgrade is a multi voltage bench power supply. For those that have been looking, bench power supplies can be very pricey, but many people overlook the ubiquitous computer power supply. It has +12v, -12v, 5v, and 3.3v taps, but not in a easy to use form. That changes with a ATX power adapter. Now you can easily add a variety of voltages to your projects, in some hefty current ratings, up to 36 amps or more.

The next stage is to add a Arduino voltage and current display for each output, and maybe some MOSFET's for adjustable output for testing motor speed.

ATX Power Supply $22

ATX Power Distribution Board $30
includes solderless breadboard and breadboard power supply adapter. 

See for notes on cooling, regulation, and other voltage combinations.
Related Posts Plugin for WordPress, Blogger...