Saturday, June 15, 2019

Using a Sensor with an ESP8266

After playing with my newly acquired Sonoff switch I got the bug to explore additional wireless uses.  This time I wanted to hook up an environmental sensor to a WiFi capable device and then send the acquired data to a Blynk app for display.  

For the controller I chose an inexpensive ESP-WROOM-02 module (based upon the ESP8266 WiFi microchip).  When the ESP module first arrived I quickly realized I would need to either purchase or make my own break-out board in order to access the pins since the unit was an SMD device.  I knew it would be quicker if I bought a pre-made adapter but what fun would that be, so I used my PC board layout software to create a custom design, then etched and drilled a copper board.  The pictures below show a finished blank board alongside a fully populated one:

Break-out board
Fully populated board
If you look closely, you can see the embedded WiFi antenna running along the upper blue region of the board.

For testing purposes I decided to use a DHT22, which is one of the many inexpensive, single wire temperature and humidity sensors commonly available on the market.

After writing and compiling my program, the next step was to apply power, then flash the code into the chip and check for functionality.  It worked (no smoke!) but I soon discovered that the WiFi module was resetting itself periodically.  After much agonizing over potential coding issues I found that many other people were having the same problem.  It seems that when the module is in transmit mode the extra current demand causes a sudden drop in voltage from the power supply that randomly forces a hard reset.  The fix was to add two, 220uF capacitors in parallel across the chip's power and ground pins to smooth out the voltage spikes.  For extra insurance I also added a .1uF capacitor to reduce any high frequency noise.  Both additions seemed to do the trick.  Pretty exciting to finally see this working!  

Update (Late Fall 2019)
My module is now officially off the grid!  I decided to add some circuitry to eliminate the need to power my module from line voltage.  Taking a 6V solar panel I had purchased years ago, I added a 3.7V Lipo battery pack and a Lipo battery charger (from DFRobot) to create a stable 3.7-4.2V reference that would work 24/7.  Next, I fed this into a 3.3V LDO voltage regulator to provide the correct voltage for the ESP module and I was all set.

Initial testing was disappointing to say the least.  The 1700mAH battery was only lasting a few hours before it needed recharging.  In idle (receive) mode the circuitry consumes ~75mA while during data transmission it jumps to over 250mA.  The poor battery never had a chance.  After researching ideas on the web I found I could perform a software activated deep sleep operation on the chip, effectively shutting down most of the ESP module circuitry for a fixed period of time.  The only part of the chip still active would be a real time clock.  In this state the WiFi module consumes less than 1mA.  Then, when the time period expires (defined in software), a specific I/O pin on the module sends a signal (pulse) needed by the reset pin to wake up the chip.  All I had to do was add a couple of lines of code and hard wire the two pins together.  I decided that sleeping for 15 minutes, then waking up and processing data for 15-20 seconds before resuming another sleep cycle would be a good compromise for my project.

The nearly completed unit, mounted to the base of a plastic enclosure is shown below.  The Wifi module is actually on the underside of the copper board so it is not visible.

Friday, May 31, 2019

Making PC Boards

I've always enjoyed making my own PC boards for custom projects but have struggled mightily with the process over the years . Either the board design wouldn't transfer or stick properly to the copper or if it did it would end up pitted after etching.  Well, I finally hit upon a formula that appears almost full-proof.  Some of this came about from trial and error on my part and some from other people's ideas.  The image below shows the results of my first board:

Not perfect by any means but pretty darn good for my needs.  I will list the steps that work for me in hopes that others might find them useful.








PC Board Production Steps
• layout board with ExpressPCB or similar software
• print design onto special toner transfer paper (print on shiny side) using laser
       printer (Staples, etc. will do this); note - I use paper from PulsarProFX but
       glossy magazine paper should work as well
prep and clean copper board (scrub with Scotch-Brite pad, wash with soap and
       water then clean with alcohol)
apply a few drops of an acetone:alcohol mixture (3:8) to board, then spread
       evenly with fingers (this mixture will cause the toner to release from the paper
       and stick to the copper); note - this mixture is the critical part of the whole
       process
position paper design onto board and gently press into place
apply a few more drops of mixture onto paper until thoroughly covered then wait
       5-10 seconds
apply even hand pressure with a paper towel for ~90 seconds, then remove towel
       and wait ~30 seconds
place board with paper into a cold-water bath for ~2 minutes then peel off paper
       backing
wait ~15-20 minutes for toner to dry then turn on laminator set on low heat
       (3mm setting, I use a model GBC H220)
once laminator is ready, cover board with green TRF film (front side taped to
       board and shiny side up); note - this material is also from PulsarProFX and
       helps to reduce pitting of the copper
run board thru laminator once then allow to air cool for ~15-20 minutes
gently remove green TRF film
patch voids with fingernail polish if needed (must be done after using green TRF
       film)
now ready for etching with hydrogen peroxide:muriatic acid mixture (2:1)

Thursday, April 25, 2019

Sonoff Smart Switches

The Sonoff series of smart switches (produced by Itead) are based upon the ESP8266 and ESP8285 microcontrollers.  They are Wi-Fi compatible thus making them useful for interfacing remotely with iOS and Android devices via a number of commonly available apps such as Blynk, eWeLink, etc.

Basic R2
For basic switching operations, these units are designed to work right out of the box using their standard, built in firmware.  All you need is to load an app onto your phone for controlling it and you're all set.  The app sends a command to the Sonoff which in turn activates a relay on the board, thus controlling the power delivered to a load.  However, I wanted to go one step further and use custom firmware to make the device self contained (no app required).

I purchased a basic model (Sonoff Basic R2) for ~$7.00 and begin experimenting with different code libraries freely available on the web.  The goal was to turn an external device hooked to the Sonoff on and off at a predetermined time each day.  I found a library that contained code for accessing an NTP (Network Time Protocol) server.  This allowed me to fetch the current time off the web.  Next, I hard coded specific times for turning my device on and off.  The drawback to using fixed times is they can only be changed by altering the program and recompiling it.  For now this will work fine, but down the road I might use an app on my Smartphone to program an on time and off time on the fly.  This, of course would require a one-time modification to the program.

In order to load (flash) the compiled code onto the Sonoff board I needed an interface between the computer and the ESP chip.  This was because the USB end uses 5.0V while the ESP circuitry uses 3.3V.  I purchased an FTDI based USB to TTL interface board from Amazon, transferred my code and was up and running in no time.

Neat little devices.

Update (June 2019)
After using my Sonoff for a while with hardcoded on/off times embedded in the firmware, I finally decided to experiment with the Blynk app on my Smartphone to allow for dynamic changes to the on/off parameters.  Blynk comes with coding examples already written for specific applications so I used one suited for my needs and simply copied and pasted the appropriate sections into my existing program.   I also made sure to store any changes to the on/off parameters into non-volatile memory to ensure the Sonoff would work when power was lost and then restored.  After loading the newly compiled code into the Sonoff chip, it ran w/o any hiccups.  So easy!  Now I can change the on/off times if needed w/o reprogramming the chip and the Sonoff will respond accordingly.

Wednesday, April 3, 2019

Wireless Communications Using 433MHz RF Modules

As a compliment to my previous project (TFT LCD clock), I decided to build another TFT/Arduino unit that would serve as a remote station.  I could then make use of commonly available 433MHz RF xmt/rcv modules and wirelessly connect between the two units.  I also wanted an excuse to experiment with these wireless devices by playing with antenna orientation, configuration (coiled vs straight), etc. to establish their maximum range and transmission accuracy.  The base station located in another room of our home would transmit time information every few seconds which, in turn would be received by the remote station located in my ham shack (currently a spare bedroom).

The RF units (STX882/SRX882) were purchased from banggood.com for ~$3.00.  Next, I located an excellent resource and code library focused on wireless interfaces (RadioHead) and used their ASK (Amplitude Shift Key) drivers for my project.  This type of modulation provides a simplified, one way channel for transmitting and receiving data with decent data integrity.
                                                                                     
433MHz Receiver and Transmitter Modules
Each RF module connects to its respective Arduino unit via a single digital I/O pin.  This allowed for easy initial checkout by directly hard wiring the transmitting Arduino to the receiving Arduino (ie. the RF interface was bypassed), then sending a test message.  After ensuring message data was properly dis-assembled by the receive station, I removed the direct connection and plugged in the RF modules.  It actually worked the first time!!!  I did not check for maximum range but it works fine for the moment with a separation of ~50ft (from one end of our home to the other) so I am quite pleased.  As shown in the following image, I also added support for an environmental sensor to display the room temperature:


Thursday, January 31, 2019

TFT LCD Clock

The market seems to be flooded lately with the versatile TFT (thin film transistor) LCD color displays so I thought it was time to put one to use in a new project.  I needed (wanted) an accurate digital display for my ham shack that could show time in two formats: 12-hr (eastern) and 24-hr (UTC).  I chose a 2.8" screen (320 x 240  pixels) with touch capability that could directly connect to an Arduino Uno w/o a shield interface.  These are dirt cheap if purchased from China (~$11.00 including shipping).  I also decided to use an RTC (real time clock) that could be programmed with the current time and date w/o having to perform any calculations.  Adafruit has an RTC module available based upon the Maxim DS3231 chip which is not only highly accurate (+-2ppm) but also capable of automatically adjusting for leap years up to the year 2100.  As a bonus, it also offers a battery backup feature so that reprogramming only needs to be done every 4-5 years or so.

The items were ordered and then I used public domain software from Github, Adafruit and others for controlling the various hardware interfaces.  I designed and  wrote the specific code for controlling the visual display elements and user interface.  The biggest challenge was selecting fonts that would "fit" into the memory space of the Uno (32kB).  I wanted a large font (55pt) for the main time display but it consumed over 5kB of memory just for the "0-9" and ":" characters alone.

Another software issue concerned the support of the different time formats (along with the additional memory demands).  I wanted to include UTC format because its commonly used in amateur radio communications when logging a QSO.  Since the eastern time zone (where I live) is 5 hours behind UTC time, every time I switched between the two time formats there would be a difference in not only the displayed hour but also potentially the day and month (actually the year could be different as well).  When eastern time format was selected, I would need to contend with another problem: daylight savings time.  Instead of scratch writing something that might or might not work (might not was the more likely outcome), I decided to search for public domain code that might address these issues.  Surely others have had these same concerns at some point?  Sure enough I eventually found a "timezone" library that handled the time differential in UTC mode and automatically adjusted the time twice a year when switching between regular and daylight savings time  (thanks to Github/JChristensen).  I simply extracted the appropriate code sections and pasted them into my code.  After a bit of tweaking everything worked as it should.

After struggling with the memory issue for a bit I finally decided to use a platform with greater memory...the Arduino Mega 2560 (256kB).  I purchased a cheap clone from China for ~$16.00 (regular price $38.00) and got started on code development.  I finally came up with what looked to me like an overall nice layout, color scheme and font selections as seen in the image below:


An added touch (pun intended) is the touch screen feature that allows me to press the screen to select between UTC and eastern time.

Tuesday, November 20, 2018

Scalar Network Analyzer

I've been working on and off for quite a while now on a network analyzer using a DDS9850 frequency synthesizer controlled by an Arduino Uno.  The details for this project can be found at this website (https://groups.io/g/PHSNA) but very briefly, a sign wave is generated by the analyzer (frequency selected by the operator) which is then sent to a device under test (DUT).  This could be a filter, antenna, crystal, etc.  The output from the DUT is then hooked to a power meter which develops a voltage proportional to its input.  This voltage in turn, is fed back to the analyzer where it is converted to digital counts.  A transfer function representative of the DUT can then be created from the collected data points covering several frequencies.  I had previously built a power meter a couple of years earlier (RF Power Meter) so I was all set.

I purchased a blank analyzer PC board, ordered the parts from various vendors and then assembled it.  Interfacing software had already been developed to run on a Windows platform so using this I fired up the hardware for initial testing.  Almost immediately it was apparent that operation was erratic from one test run to the next so something was up.  I quickly tracked this down to an intermittent solder joint on one of the toroids in the output filter stage.  Once this was corrected, everything appeared to work fine.  I was able to select a specific frequency, transmit that info to the Arduino over a USB serial interface (which subsequently communicated with the DDS module to generate a sine wave) and observe the output signal on my scope.

As a final test, I hooked up my power meter directly to the analyzer and ran a sweep function covering 1-30MHz in increments of 50kHz.  At each frequency, the power meter converted the incoming signal into a precise voltage proportional to its amplitude.  This voltage was then fed back into the analyzer for processing.  A graph was then created representing the voltage level (ie. power level) at each frequency point:


The actual "raw" output of the network analyzer is in blue and varies from a peak of ~4.0dBm down to ~3.1dBm across a 30MHz span.  This drop off in signal level is just the inherent output response of the analyzer.  To correct this, a mathematical "fudge factor" is added using a 5th order polynomial curve fitting algorithm to provide for frequency compensation and to normalize the curve to 0dBm. This eliminates the drop off and flattens out the response.  The red line represents this adjusted or compensated output.  Now, whenever a DUT is being tested a more accurate assessment of its frequency response can be made.

Update (July 2023)
I recently began making some low pass filters for a QRP transmitter I was building.  A total of three were needed (one per amateur band): 20m, 40m, and 80m. These are 7 element filters and were purchased as kits from QRP-Labs (www.qrp-labs.com) for about $5.00 each. 

20m, 40m and 80m LP Filters


After they were finished, I wanted to test each one on my SNA jig for its frequency response.  A sweep test was run for each, data was collected in a CSV file and then a graph showing attenuation as a function of frequency was created in Excel.  

Below are the results of my testing. Very nice responses overall. 



Friday, November 16, 2018

AD8307 Power Meter Update

In an earlier post (RF Power Meter), I discussed my power meter project which is based upon the amazing AD8307 logarithmic amplifier chip. This device takes a logarithmically scaled RF input and produces a linear DC output with a resolution of ~25mV/dB.

Well, I'm finally able to put this project to bed. After scratch building an initial board, I decided to go with a much cleaner solution by purchasing a pre-fabricated board from E&M Solutions. The finished board is shown below:
Completed board with RF shield covering log chip and input stage
The circuit board provides two ways to show the power level of a signal: an analog panel meter for an approximate reading plus a DC output jack for use with a DVM when I need more accuracy. In order to improve the overall resolution of the output, the board uses an op amp with a trimmer pot to produce a gain of ~2.1 to boost the AD8307 output to 5V when the maximum expected input signal is applied. The panel meter circuit has an additional trimmer pot to adjust the meter sensitivity. After adjusting both of these trimmers, whenever an input of 10dBm (10mW) is detected I should read 5.000V with my DVM and the panel meter should be at full scale.

Next, it was time to tackle the meter calibration. This required applying a known signal level, recording the output voltage, attenuating the signal by a specific amount, then recording the new output voltage. The response of the AD8307 could then be characterized and represented by a linear equation with an exact slope and y-intercept.

Previously I had built a neat little CMOS reference oscillator that puts out a square wave with a precise power level of -10dBm @ 10MHz. I used this tool to assist in the final calibration of the board. Applying this signal to the input of my meter produced 3.95V at the DVM output. Next, I removed the square wave reference and replaced it with a 10MHz sine wave oscillator I had also built earlier (Sine Wave Oscillator). I adjusted the amplitude of this signal source until it matched the power level output by my square wave reference. Now, with a sine wave of -10dBm as my signal source, I attenuated the signal by 20dB. The DVM output now read 2.87V. (Note - attenuating the signal from the square wave reference oscillator would have given inaccurate results due to design constraints with the AD8307). After a bit of math, I calculated the slope and y-intercept values. The final "response curve" for my power meter can now be represented by a simple, linear equation:

power (in dBm) = 18.5 x (voltage reading from DVM) - 83.15