RS485 Arduino Communications

There are various ways for two Arduinos to communicate information. A common way is serially via RS232. However, RS232 has distance limitations. If there is a need to serially communication over a longer distance consider using RS485 instead.  A couple of MAX485 ICs can facilitate RS485 Arduino communications over that longer distance.

Two MAX485 ICs connected together will take RS232 signals and perform the voltage level conversions required to turn them into RS485 signals and back again to RS232 signals. That will allow two Arduinos to communicate serially over a longer distance.

This chart shows the specific pin functions on the MAX485:

Here is how to connect the two Arduinos using two MAX485 ICs:

Max485 Arduino Communications

RS485 Arduino Communications

Get the Fritzing source HERE.  5V is used in this simple demonstration. For longer distances a higher voltage will be needed to power the MAX485. Also the BAUD rate will need to be adjusted (the longer the distance, the slower the speed).

This is the source code for the Transmitter:

This is the source code for the Receiver:

Here is what it looks like (messy wiring and all):

MAX485 ArduinoMAX485 Arduino

Nuttx on the STM32F103C8T6

Wouldn’t it be  C 🙄 🙄 L to have a Linux prompt on the STM32F103C8T6.  Not likely, however, Nuttx on the STM32F103C8T6 is about as close as you will get.

Nuttx is a RTOS which runs on a wide range of microcontrollers including the STM32F103C8T6.

To compile Nuttx for the STM32F103C8T6, a development environment needs set up with the right tools. My development OS of choice is a Linux distribution called Fedora (Fedora 29 is the current version at the time of this post).

To install the tools needed, I used these commands:

The first DNF command installs the ARM development tools needed to compile Nuttx for the ARM based STM32F103C8T6. The second command installs the GIT tool used to grab the source code. The third command installs other development tools necessary for the compile process.

To begin the process, create a directory to store the downloaded source and tools:

The Nuttx source code is downloaded with these git commands:

The Nuttx configuration process needs the  kconfig-frontends installed via these commands:

The reason for the aclocal and autoconf is to update the configuration files due to the newer versions of Fedoras’ development tools.

Next, create the Nuttx specific compile for the STM32F103C8T6 via these commands:

That last command should have set up everything correctly for the compile. however,  this next step allows verification of all settings and options.

A menu will appear like this:

Nuttx Configuration Menu

 

 

 

 

Optionally, verify the build setup and other options. Save and Exit when done.

This last step compiles the code and creates the nuttx.bin binary file to load into the STM32F103C8T6.

Attach the STM32F103C8T6 to a USB/Serial converter to load the Nuttx binary image:

STM32F103C8T6 with CP2102

To load Nuttx on the STM32F103C8T6 va the serial to USB converter, I used the stm32flash utility downloaded from  HERE.  Use these commands to install it:

Use this command to load the nuttx.bin file into the STM32F103C8T6:

Once loaded with the nuttx binary, the STM32F103C8T6 is ready to send the nsh (NuttShell) prompt. Connect your PC to the USB/Serial converter port with any serial communications program (115200 baud)  (like MINICOM ) and press the reset button.

Even the linux screen program can be used:

screen /dev/ttyUSB0 115200

I  like using  my Python serial program  (documented here) via the command:

For more info on Nuttx and what can be done with it, check out the Nuttx.com site HERE.

Turn an STM32F103C8T6 (BluePill) into an STLink Programmer

It is relatively easy to turn an STM32F103C8T6 (BluePill) into an STLink programmer. All that is needed is a copy of the STLink firmware and a means to flash it. Do a search for STLinkV2.J16.S4 and download it. Flash that binary file into the STM32F103C8T6.

The new STM32F103C8T6 STLink can then be upgraded to the latest firmware version from https://www.st.com.

Here is the wiring diagram on how to connect the new STM32F103C8T6 STLink programmer to another STM32F103C8T6 to program it:

BluePill as an Stlink ProgrammerDownload the Fritzing Diagram from HERE. Actually, the capacitor and 4k7 resistors on the right side of the diagram are not needed (it works fine without them).

The Arduino Command Line Interface (CLI)

The Arduino folks just came out with the Arduino Command Line Interface (CLI) :

A while back, I wrote about the  Arduino-Builder which is what the Arduino IDE invokes behind the scenes to compile the sketch. (That post is here.) Now, with the Arduino Command Line Interface, everything that can be done from the IDE can now be done on the command line.

The Arduino Command Line Interface can be downloaded from here. By the way, it is also written in the Go language.

I downloaded the file for my system (Linux 64 bit):

Extracted it:

and moved it to an easier name:

I tried it out and got it working OK for the BluePill but had to make some changes to the boards.txt configuration file.

Here is how I got it working for both the Arduino Uno and  the BluePill (STM32F103C8T6):

Create these directories first (the are needed and not created if they don’t exist):

Create a basic config file since it will need to be modified to add the BluePill info:

Edit the .cli-config.yml and add these lines at the end:

Next, create a basic test sketch (and optionally edit it):

Next, create the index json files via:

Optionally look and see what boards are available to install:

Then install the stm32duino files for the BluePill:

Optionally install the core files for the Arduino Uno (etc.):

You can see a list of the installed boards via:

If you try and compile the test sketch now via:

You will see a bunch of errors, so you will need to edit the boards.txt file:

To eliminate the errors, change some of the genericSTM32F103C lines with  the string genericSTM32F103C.menu….build. in them to just genericSTM32F103C.build. lines. Make sure there are no duplicates. (The .menu. lines are for selections in the Arduino IDE.)

Once all the correct options are fixed in the boards.txt config file, the sketch should compile fine via:

To see if the arduino-cli recognizes your attached BluePill (or other Arduino), run:

To upload the sketch:

Libraries can be found with a search:

And installed via:

……………………….

At no time did my fingers leave the keyboard… I like the cli !

STM32F103C8T6 DIY Programming Cradle

I recently created an STM32F103C8T6 DIY programming cradle. Here is what it looks like:

STM32F103C8T6 DIY Programming CradleIt comes in handy when you need to program a bunch of STM32F103C8T6s. (Note: I made a similar ‘cradle’ for programming bunches of ESP8266s’.  Read about that here.)

A standard USB to serial converter is attached to the 6 pin header shown on the right side. Here are some pictures. When the STM32F103C8T6 is ‘plugged in’:

STM32F103C8T6 DIY Programming CradleWhen it is attached to a USB / Serial converter:

STM32F103C8T6 DIY Programming Cradle

This Fritzing graphic shows how the connections are made from the USB to serial converter to the STM32F103C8T6:

STM32F103C8T6 Serial Connections

Download the Fritzing source here.

Push a Button and Play a Video

To motivate and engage sales people, have a big red button to push when a sale is made. They push a button and play a video. Something fun! Add flashing lights for extra effect!

Here are two different means to accomplish this. One solution involves an Arduino UNO serially attached to a PC running  the Windows OS. The other solution is just a Raspberry Pi running OSMC. OSMC basically turns a Raspberry Pi into an entertainment system.

A large screen can be attached to either the Raspberry Pi or a PC to display the video.  A Raspberry PI has GPIO pins which allow it to connect to the outside world. This makes it a standalone solution for this application. A normal PC has no such connections, so that is why something like the Arduino is needed.

The Arduino UNO connects to the PC via a USB to serial cable. This is how the Arduino can be wired to a button:

Push a button and play a videoDownload the fritzing source HERE.  Note: The wiring is basically the same for a larger big red button which would be easier to push (and more fun!)

Notice how the standard 4 pin push button is wired:

4 Pin ButtonWhen the button is pushed,  all four pins are connected. This is the Arduino sketch for monitoring the button push:

The  Arduino communicates serially to a Python script. The Python script, when notified of a button push, will start the video player to play the video. Here is the script:

Since that python script was designed to run on a Windows PC, VLC and Python need to be installed for it to work. The command to run VLC will need to be modified to the URL or file for the specific video to play.

The alternative solution to “Push a Button and Play a Video” is just a Raspberry Pi.  After installing the OSMC distribution,  use these commands to install the prerequisite software :

The prerequisite software is needed to allow a Python script to access the GPIO pins and interface with OSMC.  Next, wire the Raspberry Pi  to the button as show here:

Push a Button and Play a Video

Download the fritzing source HERE. A Python script (running on the Raspberry Pi) is used to monitor for the button push and run the video:

The Raspberry Pi running OSMC uses the xbmc_json python library to control  the native OSMC video player. The command to open the player and run the video will need to be modified to the URL or file for the specific video to play.

Which ever solution you choose, push a button and play a video! Fun!

Pulse Width Modulation

MicroControllers are digital devices and usually do not have built in digital to analog converters (or DACs) to output an analog signal. Some, however, do have analog input. For digital output, the voltage is one of two values. Output voltage is either 0 volts or near equal to the voltage source (‘off’ or ‘on’). Using a technique called pulse width modulation (or PWM), a microcontroller can create a ‘simulated’ analog output voltage.

Pulse width modulation turns the output on and off, each with set times at high frequencies to achieve an analog voltage. If you are unfamiliar with pulse width modulation, there are many tutorials on the internet. (Click HERE to search Google for PWM. An excellent one for the Arduino is HERE.)

Here is a short video of pulse width modulation on a PIC12F675 captured on an MSO-19 oscilloscope:

 

As can be seen in the video, the duty cycle (ratio of ‘on’ versus ‘off’)  goes back and forth from 100% to 0%.

Here is the MPLAB XC8 C source code for creating that PWM signal on a PIC12F675:

The original code was generated with my PIC Timer0 Code Generator and Calculator for a PIC12F675:

Pulse Width Modulation on a PIC12F675

and then modified to vary the duty cycle. An MSO-19 oscilloscope was used to capture the  PWM signal. If LEDs were placed on the pins of the PIC12F675, they would be seen gradually going from bright to dim and back again to bright.

 

Using pulse width modulation, an output voltage can be produced anywhere between zero volts and the source voltage. Its simply a matter of math!  For example, if  the source voltage is 5 volts,  to create a 2.5 volt output,  the output pin would be ‘on’ half of the time and ‘off’ half of the time.   (5  ÷ 2 = 2.5)  Of course the frequency of the switching cycle has to be significant.

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.

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.

PIC12F683 with LDR

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.

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.

Load more