Feb 15 2018

PIC Timer0 Code Generator and Calculator

I needed an easy way to calculate Microchip PIC timer 0 values, so I created the PIC Timer0 Code Generator and Calculator.  If you are not familiar with PIC timers, there is a handy tutorial HERE.

The calculator part, is a spreadsheet which calculates the timings and timer 0 frequency counter needed, given the  required timer (delay). The code generator part, is a Python script which generates the C source code for the PIC microcontroller XC8 compiler.

Here is what the spreadsheet looks like:

Basically, the required timer (i.e. delay) in seconds is specified along with the clock source in Mhz and the prescaler. Optionally a value can be specified for the TMR0 preload to adjust the timing.  All the other values are automatically calculated.  From the example, a 1 second delay is specified with a clock of 4Mhz,  prescaler is 32 and preload is 6.  The calculations show that 125 timer 0 interrupts will take 1 second.

The spreadsheet calculator can be downloaded from HERE.

In these examples, I use the PIC12F675 programmed with a PicKit2 programmer:

PIC Timer0 Code Generator and CalculatorPIC12F675

 

 

 

 

However, even though I use a PIC12F675 for the PIC Timer0 Code Generator and Calculator, it can easily be adapted to any other PIC microcontroller which needs timer 0 calculations. Actually, the spreadsheet part works works for any PIC since it does not generate code. The Python script, which generates code, would only need minor changes for other PIC microcontrollers.

Here is the Python script (gentimer0.py) which generates the C code:

Here is the command to run it:

and here is the resultant output:

The source is complied (via the Microchip XC8 compiler) with this command:

Then use the PicKit2 programmer:

PICKit2 Clone Programmerwith the command:

to send it to the chip.
PIC Timer0 Code Generator and CalculatorNotice, that /opt/microchip/ is where I have installed he XC8 compiler and the PicKit2 software (pk2cmd).  If you have an LED on GPIO pin4, it will blink every 1 second. Basically with this code generator, one has a starting point template to add to. The comments in the code show where to put code to make it more useful.

Feb 05 2018

Controlling Things Based on Light Intensity

A PhotoCell or LDR (Light Dependent Resistor), used in a circuit, allows controlling things based on light intensity.

PhotoCell

PhotoCell / LDR (Light Dependent Resistor)

Here is a circuit showing how to wire an LDR and demonstrates how controlling things based on light intensity is possible with a little help from a microcontroller.

Controlling Things Based on Light Intensity

Controlling Things Based on Light Intensity

The thing being controlled in this circuit is simply an LED which turns On or Off. However, it could just as easily be a relay to control some other device. (Get the Fritzing source HERE for the above circuit graphic).

An analog value of the voltage which varies with light intensity can be read by a microcontroller. In this circuit, a PIC12F683 microcontroller is used. Based on that value, other circuitry can be controlled (i.e. turned On or Off).

Here is the MPLAB X IDE C source code for the PIC12F683 in the circuit shown above.

The analog value varies from 0 to 1024 with 0 being no voltage and 1024 being max voltage. The code shows turning on the LED if at least 1/2 of the voltage  (1/2 of 1024 = 512) is being passed by the LDR. Since the source voltage is 5V,  1/2 of it would be 2.5 volts.  Depending on the application for this sort of circuit, some experimentation can be done to determine the optimum value for when to turn something On/Off.

The data sheet for the PIC12F683 is HERE.  It is essential reading in order to understand how to write code for the chip. Native C coding (using the MPLAB X IDE) for PIC microcontrollers is a bit harder than the ‘sheltered’ environment of the Arduino IDE.

Yes, you can do this sort of thing with an Arduino (and the code would be easier to develop) but at a higher component cost. The components in this sample circuit are minimal, small and low cost.

Jan 27 2018

How to keep an ENC28J60 Module from Freezing

The ENC28J60 module is good for attaching an Arduino to a LAN or the Internet. The one real problem in doing so is how to keep an ENC28J60 module from freezing. No, not temperature wise ! 🙄  (‘Freezing’ in the sense of locking up, stop working, etc.)

How to keep an ENC28J60 Module from Freezing

ENC28J60 LAN Module

There are many Ethernet  arduino libraries for the ENC28J60 module, however,  I am using the UIPEthernet library from HERE. It is updated from the original Ethernet and follow on UIPEthernet libraries. Documentation on the Ethernet library can be found HERE along with sample code. Note: The UIPEthernet library I am using fully supports all the documented functions from the original library.

There are various ERRATA on the ENC28J60, however, the current versions of the UIPEthernet  library address those issues. This library also supports the STM32F103C8T6. Usually one would think that such ‘lockup’ or ‘freezing’ problems are related to power problems. That problem is not an issue here, it is taken care of by testing with different good power sources and lots of capacitance.

I have the most trouble when I attach an STM32F103C8T6  to the ENC28J60 module.

ARM Cortex M3 STM32F103C8T6

STM32F103C8T6

I seem to have the least problems when I attach an Arduino UNO to it.

Arduino UNO

Arduino UNO

I think that perhaps the extra speed of the STM32F103C8T6 enhances the problem. In the main loop() code, the library function checks on the ENC28J60 and perhaps due to the faster speed of the STM32F103C8T6 there is something going on that affects the ENC28J60.

Another, less know issue, is the need to occasionally call the library function maintain(). If one looks at the documentation, it seems maintain() is only used to renew a DHCP lease. Further research will show that this function call is needed, periodically, if connected to an ENC28J60.  I found that there are fewer ‘freezing’ problems when calling that function in the main loop() code, however, they still occasionally happen.

The best approach to fully solving the issue is to somehow check the status of the ENC28J60 and reset it if it is ‘frozen’. Fortunately, the code for that is available HERE.

This is my sample WEB server code which shows how to keep an ENC28J60 module from freezing by implementing that checking of the ENC28J60.  It also shows the use of the maintain() function and the use of the STM32F103C8T6 watch dog timer. I use the watch dog timer to make sure the mictocontroller running the code to control the ENC28J60 is not somehow locked up (‘frozen’).

Here is the code:

From the ASCII chart in the comments at the top of the code, one can see how the STM32F103C8T6 is wired to the ENC28J60.  One thing to note, is that the RESET of the ENC28J60 is accomplished via its RESET pin rather than by calling the Enc28J60.init() function.  This does a hardware reset and seems to work in all lockup cases.

Jan 25 2018

Switching 12V from a 5V Arduino

I built a circuit to allow switching 12V from a 5V Arduino. Here is the wiring diagram and a picture of the actual circuit:

Switch 12V from 5V ArduinoSwitching 12V from a 5V Arduino

 

 

 

 

Get the Fritzing source HERE.  The side with two connections attaches to the 12V device being switched ON/OFF. The side with three  connectors,  connects to the 12V source and Ground. It also connects to an Arduino PIN to do the ON/OFF switching.

The actual circuit (pictured above) is doubled and can handle switching two 12V devices.  A PC power supply (converted to a ‘bench power supply’) can be used to power the Arduino and the 12V devices,

You can hook up a PC power supply and make a bench power supply with an adapter. That adapter easily attaches to the power supply and breaks out the 12V, Ground, 5V and 3.3V sources. Pictured below is an example of such an adapter:
PC Bench Power Supply AdaptorThe circuit switching 12V from a 5V Arduino  comes in handy for a number of things. I am using it to turn on a 12V Emergency Alarm.  Here are the two types (siren and siren with strobe) that I am using. They both work with 12V and Ground.

Switching 12V from a 5V ArduinoSwitching 12V from a 5V Arduino

 

 

 

 

 

This circuit switching 12V from a 5V Arduino is part of a much larger home/office alarm system project I am working on.

Jan 21 2018

Decoding an ESP8266 Firmware Image

I recently coded a Python script for Decoding an ESP8266 Firmware Image. It’s the first part of my quest to create a Linux tool for creating a single flash image.

More specifically, the script will decode a single binary image or a ‘combined’ image. A combined image (single flash image) can be created with the flash download tool from Expressif. (Exressif is the company which makes the ESP8266.)  However, that download tool only runs on Windows. Here is what the Expressif Flash Tool looks like:

 

ESP8266 Flash Tool on Windows

Notice, the ‘CombineBin’ button. It is used to create a single binary image from the multiple files (‘segments’). In that image you can see seven files (the current SDK 2.1.0 files as of this post date) listed and the addresses to load them . I used the ‘CombineBin’ button and created a single binary image called sdk-2.1.0.bin. (The default file name it creates is called target.bin.)

I run Linux. and the recommend ESP8266 tool for it is esptool.py. The esptool script, however, does not have a option to create a single combined image. It may have that feature as an enhancement at some future time. Hence, my quest, to develop a Linux tool to create a single flash image.

I like to keep my ESP8266 firmware current. To flash the latest SDK, I use this script:

While that works fine, I think it would be more efficient to create a single combined image to flash. Especially since I have a number of ESP8266 modules to flash. The single image (sdk-2.1.0.bin created from the windows tool) can be flashed as follows:

To create a tool to combine all the ‘single’ images into one, one needs to know the format of the single images and the format of the ‘combined’ image.

Espressif has a brief document (HERE), describing the firmware image format. However, that document does not fully cover what is need to decode a combined image. Also, it does not cover all of the single image ‘formats’. Esptool.py has an image_info command, however, it only works on single modules. It does not work on combined modules.

In my ‘reverse engineering’ attempt at Decoding an ESP8266 Firmware Image, I created this Python script called esp8266_parse_bin.py. Here is the code:

Esp8266_parse_bin.py can be used to decode any of the single modules that comprise the combined sdk-2.1.0.bin image or it can be used to decode the ‘combined’ image.

When it is used to decode the combined sdk-2.1.0.bin image via:

Here is what the output looks like:

From that output, one can see where (address) and how each of the single images are placed in the ‘combined’ image.  Now that the combined image format is known, a script can be coded to create it from the individual files.

Oct 22 2017

12V to 5V Sensor Circuit

A number of sensors are powered via 12 Volts. Some, however, need 5 Volts.  To power a remote DHT11 temperature sensor, I created a 12V to 5V sensor circuit.

This is the wiring diagram:

12V to 5V Sensor Circuit

Get the Fritzing code for it HERE.

DHT11

DHT11

 

 

 

The DHT11 needs 5 volts,  ground and data. It plugs into the 12V to 5V sensor circuit via a 4 pin header slot. Notice there is a 10K pull-up resistor connected to data.

Here is what the circuit looks like with and without the DHT11 plugged in:

12V to 5V Sensor Circuit Side View12V to 5V Sensor Circuit with DHT11

This type of circuit comes in handy for a larger project such as an alarm or monitoring system where sensors are some distance away from the main monitoring device such as an Arduino.  For longer distance cable runs, Cat 5  cable is used which has 8 wires.  Three wires can be twisted together for 12 volts. Three wires can be twisted together for ground and the remaining 2 wires can be twisted together for data.  The multiple wires together increases the ‘wire size‘ and overcomes signal loss and interference in most cases.

Sep 02 2017

4 Character 7 Segment Display

I built another handy circuit to ‘house’ a 4 character 7 segment display. It looks like this:

4 Character 7 Segment Display

Basically it just ‘breaks out’ the 16 pins of the display with two 8 pin female headers. Pins 1-8 are connected to the header at the bottom and pins 9-16 are connected to the header at the top. Note that 6 220 Ohm resistors are needed to protect the LEDs.

 

 

The Fritzing diagram to the right (Download Link) shows which pins select which character (colon or apostrophe) is selected to turn on and thus needs a resistor. The LEDs actually light a bar shape. Each ‘character’ is composed of 7 LEDs (or segments). The image below shows what each of the pins controls.

4 Character 7 Segment Display Pins

 

 

 

 

The LED pattern is as follows:

 

7 Segment Display4 Character 7 Segment DisplayFour of these characters go together to be able to display things like time, temperature, voltage, etc.

 

 

The technical manual is HERE. The pin functions (1-8 bottom and 9-16 top) are as follows:

Connecting this 4 character 7 segment display to an Arduino is usually done in conjunction with 1 or more  shift registers due to having so many wires. Connecting without the shift register, an arduino looks like it needs a ‘hair cut’ with so many ‘wires’ all over the place.

4 Character 7 Segment Display

4 Character 7 Segment Display

 

 

 

 

 

 

 

I connected my 4 character 7 segment display circuit with two 74HC595 (shift register IC)  breakout modules. Each 74HC595 module controls 8 pins (1 shift register for the bottom 8 pins and one for the top 8).  With these modules, only 3 wires (besides power and ground) connect to the Arduino.

4 Character 7 Segment Display

 

Using an Arduino library designed for the 74HC595 shift register makes it easier to control the display. I like this shifter library hosted on GitHub. With it, each pin is easily referenced by number.

 

 

Here is a an Arduino sketch I created to display current voltage on the 4 character 7 segment display :

Notice that I created functions to select the specific character to turn on and a function to turn on the LED segments needed to display each number digit. With the shifter library and these functions, you are all set to make something interesting! How about a multimeter?

Aug 28 2017

I2C 2-wire Circuit

My latest project involves collecting data from a number of sensors connected to multiple Arduinos. I2C (or 2-wire) is a perfect fit for this project because I needed an easy way to connect the Arduinos and aggregate the data. With I2C, I can have one Arduino as the master and several other Arduinos as slaves.  To connect the Arduinos together, I created a simple I2C 2-wire Circuit.

Here is the finished product and the wiring diagram:

I2C 2-wire CircuitI2C 2-wire Circuit

 

 

 

 

 

You can download the Fritzing source for the diagram HERE. As you can see from the diagram, it allows multiple connections to Power/Ground and multiple connections to SDA / SCL (the two wires used in I2C). SDA (serial data line) is used for sending data and SCL (serial clock line) is used for clocking.  I2C allows for multiple master/slaves. I have one Arduino as the Master and several Arduinos as slaves. I will eventually use a Raspberry PI as the master.

If multiple Arduinos are connected on a breadboard, there can and probably will be some voltage loss. This is especially true when connecting breadboards together and ‘jumpering’ power/ground from one breadboard to another. It is best to supply each breadboard with its own source to power and ground. I built this little testing circuit to get around the power loss problem. Notice that I used 4.7K pull up resistors in this I2C 2-wire circuit. These resistors are needed to ensure the I2C bus works properly.

Jul 03 2017

ESP8266 DIY Programmer Cradle

If you have been reading my posts lately, you will notice that I have been working a lot with the ESP8266 wireless module. To make it easier to work with, I made an ESP8266 DIY Programmer Cradle. Here is what it looks like (without and with the ESP8266 plugged in):

ESP8266 DIY Programmer CradleESP8266 DIY Programmer Cradle

 

 

 

 

 

You may notice that I made several of them, some with a 100uF capacitor between Volts and Ground and some without. There are two jumpers, one for GPIO00 and one for GPIO02. When programming the ESP8266, GPIO00 is jumpered to Ground (otherwise it is ‘floating’, i.e., not jumpered). An LED is attached to GPIO02 for testing. Take a look at my MicroPython Web server script mentioned in this post to see how to toggle the LED On/Off. The 6 pin header on the side is connected to a USB to Serial adapter for power and/or programming.

The wiring for the ESP826 and the ‘cradle’ are shown in these graphics:

ESP8266 Wiring ESP8266 DIY Cradle Wiring

 

 

 

 

 

 

Get the Fritzing diagram source HERE. The ESP8266 DIY Programmer Cradle is powered by a USB to Serial Adapter (jumpered to 3.3V) like the one shown below:

USB to Serial ConverterIf you add a power pack (for example, one that is used to charge a phone):

ESP8266 with USB to Serial Converter with Powerbankyou’ll have a portable ESP8266 based wireless module. In any case, with the ESP8266 DIY Programmer Cradle, I have an easy and fast way to plugin, program and test the ESP8266 modules.

Jun 30 2017

MicroPython WebREPL on Android

I created an app (apk) to run MicroPython WebREPL on Android. Imagine you have your ESP8266 (with the MicroPython firmware) in some location where the only access to it is via a wireless connection. If you need to access and change/control it, you will need WebREPL.

Of course, a wireless enabled laptop would get access to WebREPL if you have it installed locally. However, if you are connected to the ESP8266, you might not have internet access to remotely load the WebREPL code.

Alternatively, an Android phone provides the ultimate in portability but has no file system (per se) to store and load the WebREPL code from a browser like the laptop would. You probably could load the WebREPL code in a browser while connected to the internet, then switch over the WiFi to the ESP8266. However, I prefer an app.

So, to make WebREPL available thru an app on Android, I used Apache Cordova to create it. This is a ‘screen shot’ of how it appears on the phone:

MicroPython WebREPL on AndroidIf you are interested in the complete Apache Cordova project code to access the MicroPython WebREPL on Android, download it from HERE. If you are only interested in the apk (app) file, download it from HERE.

The current version of WebREPL uses a no longer supported version of an xterm emulator called term.js  found HERE. While that version works on a PC browser and a browser (like Firefox) in Android, it does not work right inside of an Android app. The keyboard does not function right. So I replaced it with its predecessor called xterm.js found HERE.

If you are interested in the steps I took to create the app, here is what I did:

Those commands will create the basic code for an app. Next, I download and unziped the code from webrepl and xterm.js.

Next, I customized the config.xml to add the MicroPython icon by adding this line:

Then I download the png graphic and  put it in the micropython directory with the name MicroPython.png.

Next, I went to the micropython/www directory and removed everything there. Then I copied over to it these files from where I unzipped the webrepl code:

I renamed webrepl.html to index.html. Next,  I copied over to it (micropython/www) all of the files and directories in the dist directory of the unzipped xterm.js. Then, index.html needed to be changed a bit to use xterm.js vs term.js.

Finally, I built the apk with the command:

and installed it in the phone. After connecting the Android phone wirelessly to the ESP8266 to get an IP address, the MicroPython WebREPL on Android apk is ready to run!

Load more