STM32 Blue Pill PCB
Overview and Objectives
The STM32 OnStep was conceived with these objectives in mind:
- Low Cost, using inexpensive commodity components available from multiple sources on eBay, AliExpress and other marketplaces.
- High Performance, the STM32F103C8T6 much faster than the Arduino Mega2560 (~ 4.5X), allowing faster slews (with mode switching, i.e. microsteps slewing), 0.75X as fast as Teensy 3.2). The F303CC is even faster at ~ 9.5X the Arduino Mega2560.
- Relative Ease of Build, avoiding discrete components as much as practical.
- Real Time Clock, as a result of using an RTC module with EEPROM, the STM32 will keep the date and time indefinitely.
OnStep was ported to the STM32 platform by Khalid Baheyeldin, with valuable assistance provided by many people, including author of Onstep Howard Dutton, Dave Schwartz, Pierluigi Giorgi, and others.
Before you buy components, or connect any of them please read the entire page carefully.
Board Variants: The STM32F103C8T6 Blue Pill, and RobotDyn STM32F303CC are the only supported boards for the Blue Pill PCB. The latter is more than 2X faster than the original Blue Pill, and has double the flash memory, so it is future proof. It is recommended that all new users go with the F303CC.
USB: Tested, works. You can use OnStep's extended command set, which is based on the popular Meade LX200 command set, to control the mount from Planetarium programs like Stellarium, Cartes du Ciel, KStars, Sky Planetarium
WiFi: Works with WeMos D1 Mini (recommended), ESP01 (requires wiring changes to 3.3V power instead of the default 5V wiring below), and NodeMCU Lolin (works, but too large of a footprint).
Bluetooth: Works with an HC05 6-pin module from eBay. This is explained in this post.
Stepper Drivers: The LV8729 (for low current motors) and S109 (for high current motors) are verified to work, and recommended. The TMC2130 are also recommended. They must be the SPI variant with M0/M1/M2 pins down. The ST820 are also verified to work. The low cost DRV8825 also works, but at a higher MaxRate (hence slower slewing speed, all else being equal). A4988 is reported to work, but it has a limited upper limit for microsteps. External stepper are supported and require the removal of some lines in the Config.h file.
Focuser: support for one focuser motor is provided (Axis4 in OnStep).
ST4 Port: Confirmed to work with both a simple or a smart hand controller (SHC), and pinout is compatible with other OnStep boards.
Tracking LED: Supported. When the mount is tracking at sidereal rate, the LED will blink continuously. See the detailed assembly instructions for making an external LED work.
Buzzer: Supported. At the start of a slew, and the end of slew, a buzzer will sound.
Temperature and Pressure Sensor: The BME280 I2C Temperature/Pressure/Humidity sensor is supported. It provides more accurate inputs for the calculation of refraction and full compensation. Details in this post.
Limit Sense: Supported. A pin is provided to detect when limit switches are tripped.
PEC Index Sense: Supported. A pin is provided to detect the worm gear's index, for recording and playing back PEC (Periodic Error Correction).
Reticle: A pin is provided to turn on the light in the polar scope and/or an illuminated eyepiece. Needs some modifications to OnStep to make it work.
The STM32 family of ARM Cortex 32-bit processors that are low cost, yet provide adequate processing power.
Of that family, the STM32F103 runs at 72MHz (that is 4.5X the Arduino Mega2560).
The so-called Blue Pill is the main microcontroller for the STM32 port of OnStep. It is based on the STM32F103C8T6, with 128KB flash memory, and 20KB RAM. The cost for these is around US $2.50 on eBay and AliExpress. Although many boards state that they have 64K of flash (too small for OnStep), they do come with 128KB.
Support is in the current stable branch (3.16 and later).
The pinmap supports the following:
- Two motor axes (Alt/Az, or RA/DEC)
- Ambient weather using BME280 module
- RTC (using ZS-04/DS3132 I2C module... also contains required EEPROM)
- WiFi (Bluetooth works too using an HC-05 module),
- ST4 guiding port, also usable by hand controllers (both basic and Smart Hand Controller).
- One LED pin for tracking
- PPS (Pulse Per Second) from the RTC, using the SQW (Square Wave) pin.
Other features (e.g. PEC index sense, Limit sense, Reticule for Polar Scope) can be supported using the spare pins for the Blue Pill.
Here is an overview video, when the project was on a breadboard.
Bill Of Material
Here is a complete bill of material, with the approximate cost, as on eBay. AliExpress would have similar prices:
At a minimum, you need the following components, for a minimal working system:
1 X STM32F103C8T6 Blue Pill ($2.5)
1 X DS3231 RTC Module with EEPROM (~ $1.5)
1 X LM2596S 3A Step Down Buck Converter Module (~ $1.5).
1 X CP2012 UART to TTL microUSB board (~ $1.5), for USB
2 X Stepper Drivers for Axis1 (RA/Alt) and Axis2 (Dec/Az), see models supported below.
2 X 50V 100uF electrolytic capacitors, to protect the drivers from back voltage if the motors are turned manually.
2 X Motors, Exact specs will vary, depending on your mount's physical dimensions, required torque, current and voltage. Check the Construction page on the Wiki for more details).
1 X 3A mains AC to 12VDC power supply. It has to be a regulated power supply, not the old cube style unregulated "wall warts". Input voltage can also be 24V, if you adjust the LM2596 correctly.
You will also need a perfboard or stripboard to affix the components to, female pin headers, wires, and a case.
As well, you will also need connectors for the motors, such as RJ45, female DIN or mini-DIN connectors.
1 X glass fuse, 5x20 mm (3A), or use a case mounted screw-in type fuse
1 X WeMos D1 Mini WiFi Module (~ $3.5), for WiFi control of OnStep using the Android App. Highly recommended.
1 X 3.3V buzzer, active or passive (each has a different setting in Config.h). If you use the passive buzzer, you will need a 470 Ohm resistor.
If you want a focuser, you will need:
1 X Stepper Driver
1 X Stepper motor
1 X 50V 100uF capacitor
If you will be using the ST4 for autoguiding, or for a Smart Hand Controller, you will need:
1 X 2.2K Ohm SIP-5 bussed resistor network/array for the ST4 port. If you are experienced, you can make your own using standard resistors, for example, like this one which used surface mount resistors, or use through hole resistors for easier soldering.
The recommended stepper driver is the LV8729. This comes in two versions, the MKS LV8729 Stepper Driver, which is orange with green pins ( ~ $7 each), and blue board with blue pins. Both are verified to work.
SilentStepStick TMC2130 or TMC5160 (read about these drivers before buying!) are the most advanced stepper drivers supported (operates in SPI mode.) For the PCB, jumper J1 should have the two left pins (closest to the RTC) connected, and J2 should have the two right pins (closest to the WeMos) connected.
The generic DRV8825 Stepper Drivers ($2 each) are also supported, but are observed to be more noisy, and perhaps have more vibration. They may also need a higher MaxRate (therefore, slower slewing speed). For the PCB, jumper J1 should have the two rightmost pins (closest to the focuser axis) connected, and J2 should have the two leftmost pins (closest to the LM2596) connected.
Other driver models that should work, but are not tested, include: A4988, S109, ST820, TMC2208 and TMC2100.
The easiest way to built an STM32 controller is to order this printed circuit board (PCB), and the components above, as well as some miscellaneous connectors, and solder them on the ready made PCB. To order, click on Open in Editor, then click on the G icon (for Gerber files) on the top. An order form at JLCPCB will pop out, and you can select the quantity and features.
Kits that contain all the connectors, pin headers, fuse holder, as well as the required modules are available from Khalid Baheyeldin and George Cushing. These kits will ship and arrive faster than if individual components are ordered from eBay or AliExpress. They are provided at near cost, as a service to fellow amateur astronomers.
You can also view the detailed assembly instructions for the PCB, and download it as a PDF.
Here is a brief video on the PCB, and another video of a Vixen SXD driven by an STM32 PCB.
And there is a detailed video by Aisling Lightworks on how to solder all the parts to the PCB.
And this is the bare PCB (version 1.5 shown... current version is 1.8 but very little has changed except minor positioning and silkscreen labeling):
And here is a populated PCB:
It still has no fuse holder, no MULTI header (for PEC and Reticle to mount), ...etc.
If you use the PCB or a full kit, skip the next few sections, up to WiFi.
If you not using the PCB, and building your own perfboard, breadboard, read on.
In case you want to build your own board, here are the schematic diagrams. Extra information is present in the comments of the pinmap.
Below is the Blue Pill:
The following schematic parts are for the mandatory components only, without which OnStep will not run on the STM32.
Axis 1 (RA or Alt) and Axis 2 (Dec or Az):
RTC and EEPROM
WiFi (although optional, it makes controlling OnStep from an Android device much easier):
You can use a cardboard box waterproofed with packing tape as a temporary case for your finished STM32 PCB.
You can also adapt a regular plastic case to enclose your finished STM32 PCB. For example, this cheap Dry Box at Walmart worked well for some users.
There are several designs for a 3D printed case for the STM32 controller.
This one is built using the Universal Project Box, and so is this other one.
The other one is a custom box that needs 4 screws but is designed to be stackable with other boxes.
Another design by Roman Hujer.
See this thread for more designs, as more people share them.
For those who have the skill and patience to make their own perfboard layout.
It is very important to note the following:
- There are jumpers for each axis between the SLP and RST pins. These allow different driver models to be used. For the LV8729 don't connect any jumpers. For the DRV8825 connect SLP and RST. These jumpers also allows the TMC2130 SDO pin to be connected to the associated MISO pin.
- The DS3231 RTC module has its 90 degree pins straightened out, so the battery is facing up, and the pins are aligned to the I2C pins of the STM32.
- The CP2102 USB module also has its pins straightened out, and component side is up.
Please note that the Blue Pill alone, will not run OnStep! At a minimum you need the DS3231 RTC module, and the CP2102 USB to TTL module. This is because OnStep requires the EEPROM on it for storing its configuration, and upload mode must be "Serial" in the Arduino IDE.
In general the steps are as follows:
The LM2596S input is connected to to the 12V or 24V input from the regulated power supply. Before connecting any electronics, the output for the LM2596S has to be adjusted using a small screw driver to turn the potentiometer, with a multimeter measuring the output. Output should be 5V.
For the stepper drivers (LV8729 or DRV8825), you need the FLT pin on the driver to be connected to 5V. Both the SLP and the RST pins are to be connected to each other.
For each stepper driver, a 100uF capacitor should be connected between VMOT and GND, as close to the stepper driver as possible. The side marked negative on the capacitor should be connected to the GND.
The DS3231 RTC module would fit better if the pins are straightened out (rather than being at a right angle). That way, the component side of the module will be facing down, and the battery facing up, making it easier to replace. By doing this, the RTC module does not consume vertical space.
For the USB connection, the above mentioned CP2102 UART to TTL microUSB board must be used for the Blue Pill. This allows the use of regular microUSB cables without the risk of damaging the STM32 or other components.
Before you wire the CP2102 module, you have to straighten the pins out (rather then being at a right angle). Now, the component side faces up, and the CP2102 does not consume vertical space. Also, the TX and RX pins of the module will be aligned with their counterpart pins on the STM32.
Wiring is by connecting the PA9 TX pin on the STM32 side to the RX pin on the CP2102 microUSB board, and PA10 RX to the TX pin on the CP2102.
Connect the GND pin on the board to the GND on the controller.
Do not connect positive voltage from the OnStep board to the CP2102 board.
And you must never connect a regular USB cable from your PC to the STM32's microUSB port directly.
When you connect the CP2102 to the PC you will have an entry like the following in your Device Manager. For a first-time connection, Windows will download the correct driver from the Windows driver database. Make note of the COM port number - you will need it when configuring the Arduino IDE for uploading the software.
The PCB has the correct pins for the WeMos D1 Mini, as well as power for it. No hardware changes are needed.
The WiFi-Bluetooth component does not need any changes in Config.h for it to work on the WeMos D1 Mini when it is placed on an STM32 PCB.
Input power may be higher than the motor's specified voltage. That is acceptable, as long as the stepper driver limits the current to the motor.
To limit the current, you have to do this mandatory and important step, before connecting the motors to your controller. If you do not do this, the drivers and motors may overheat. The exact value to use depends on the specific stepper driver model you will be using, and the motor electrical specifications. Here is a good Vref overview for the different driver models.
Use a multimeter, and a small screw driver, adjust the potentiometer on the stepper driver.
Watch a Youtube video before you start.
Installing Arduino IDE for STM32
Download and install the standard Arduino IDE. You should use version 1.8.5 or later.
There are two different board managers for the Blue Pill, depending on the version of OnStep you are using, as follows:
OnStep 3.16 and Earlier
First, you will need the ARM Cortex M3 compiler and toolchain. To do this, go to Tools -> Board -> Board Manager, search for "Due". Install Arduino SAM Boards (Cortex M3).
Then, download the Arduino_STM32 board manager, and extract it under the Arduino\hardware directory, and rename the directory from Arduino_STM32-master to Arduino_STM32.
OnStep 4.15 and Later
For OnStep 4.x and later, you have to use the STM32Duino Board Manager.
Use the instructions here to install it.
The advantage is that the resulting OnStep binary uses less flash storage, and therefore can accommodate most of the extra features that were included in OnStep (e.g. Spiral Search). For example, on a typical configuration, version 4.x compiled to 91% of the 128K flash space with the newer STM32Duino, vs. 99% for the older Arduino_STM32.
Regardless of the board manager that you installed, you still need to install the following:
A) The Makuna RTC library. In the Arduino IDE, select Sketch -> Include Library -> Manage Libraries, then search for "Rtc by Makuna", and install it.
B) If you are using OnStep 3.16 or earlier, then you also need the digitalWriteFast library from Watterott. It has to be installed manually. Download the zip and extract it, then move the directory digitalWriteFast to Arduino\libraries. For Linux, the procedure is detailed here. This is not needed for OnStep 4.20g and later.
The STM32 is supported in all current versions of OnStep. You should use the latest stable version (currently 3.16) if you are just starting with OnStep.
You should extract the zip file to your Arduino directory (on Windows, this is Documents/Arduino ), and rename the directory to 'OnStep'.
Before you flash OnStep to the microcontroller, you have to use this Spreadsheet and enter your motor and gear parameters. Then put the results in Config.h (at a minimum STEPS_PER_DEGREE and MICROSTEPS for both AXIS1 and AXIS2). Enabling tracking autostart makes initial testing easier.
You can use the online configuration generator (OCG) which supports both version 3.16 and the master (4.x) version. It is helpful to generate a basic configuration with the most commonly used features and parameters, but you may still need to tweak the Config.h file it creates.
Important: For best performance, you need to use mode switching, that is, a lower step for slews than for tracking. If you do not use mode switching, your slew speed will be limited to a MaxRate of 32. If you do use mode switching, you can use a MaxRate of 16 for faster slewing. To do this, in the generator, if you choose 1/16 or 1/32 for tracking, you need to choose 1/2 or 1/4 for slewing. The exact value depends on many factors (motors, drivers, gears, ...etc.) and you will need to experiment with what works for your particular mount. Some report success with 1/1 for slewing and 1/32 for tracking. Others 1/2 slewing and 1/16 tracking. There are special considerations for the TMC2130, where there difference should only be one step apart. For other drivers, you can try wider differences. See this thread for details.
Save the generator's output to the directory named OnStep.
Flashing OnStep to the STM32
Both boards require that you have a microUSB UART to TTL adapter for flashing of OnStep, as well as for connecting OnStep to a personal computer.
To do this, the UART to TTL adapter should be connected as explained above. Once again: do not connect either 3.3V or 5V, otherwise you will damage your STM32!
Then, move the jumpers on the STM32 board so Boot0 is +. Leave Boot1 connected to -.
If you are using Arduino_STM32, then in the Arduino IDE, select the 'Generic STM32F103C series' board type from the 'Boards manager'. As of version 1.8.13, where the individual board types have been nested inside submenus with the name of the package from which they were installed, this looks like:
Use the following parameters when flashing OnStep to the board:
Board: Generic STM32F103C8
Variant: STM32F103CB (20K RAM, 128K Flash)
Upload method: Serial
You should also select the port that is connected to your STM32 CP2102 microUSB port.
Again, this looks like the following in version 1.8.13:
If you are using STM32duino, then the options are different.
The first step is to select the ''Generic STM32F1 series" option from the 'STM32 boards' group of board managers:
Then from the 'Generic STM32F1 series' menu, select the 'BluePill F103CB (or 8 with 128k)' subtype:
And finally, select the following options. The only ones you should need to change are the STM32CubeProgrammer type from SWD to Serial and to select the COM port assigned to the CP2102 when it was connected. Note that it is the omission of the USB support (the USB port is not used in the BluePill OnStep environment) that gains back the extra flash space.
After you have flashed OnStep, set the STM32 jumpers so both Boot0 and Boot1 are connected to '-' (or move the Flash/Run switch on the STM32 PCB). Note that moving the jumpers or switch must be done while the board is not powered and the CP2102 is disconnected from the PC (on Windows, the driver does not recognize the change of state otherwise and a connection to OnStep via the USB port would not be successful).
Flash Memory Capacity
The STM32 Blue Pill has 128K of flash memory. This runs most of the features supported in OnStep 3.16 but is still bumping up against the limit. For example, while it is possibly to have both a BME280 weather sensor and focuser, it is may not be possible to add a OneWire temperature sensor to create a point-of-use, temperature-sensitive dew heater. You may need to experiment with a combination of features to get one that fits within the memory limitation.
With OnStep 4.x more features are able to be used due to reclaiming that flash used for USB support, however it is still close to the edge and cannot be guaranteed that a set of features which fits now will continue to fit in the future as the underlying code continues to expand. For example, DEBUG VERBOSE uses a large amount of memory and configurations using more than a few of the basic features will be unable to use it even temporarily.
Testing The OnStep Controller
Do not try to test OnStep before you have the DS3231 RTC connected. This module provides an EEPROM which is required by OnStep. Without this module, OnStep will not work with STM32.
The easiest way to test is to connect the controller to a PC running SkyChart (CdC) or another Planetarium software that supports the LX200 command set. You must connect the USB port as instructed above.
You can also test over WiFi by connecting to the network named ONSTEP, then browsing to: http://192.168.0.1/
If you followed the above steps, but your STM32 controller is not working, then review the following points:
- There has been reports in late 2019, and 2020 on the failure of some STM32 Blue Pills to flash correctly. The first thing to check if you face such a problem, is the R3 resistor on the bottom of the board. The modules that fail to flash often has the wrong value in this resistor (100k ohm, instead of a 10k ohm), with the marking 104 or 01D on it. This causes the module to ignore that Boot0 jumper is in flash mode. If this is the case, you can replace the R3 resistor with a solder bridge or a short wire. You may also need an extra step, which is to remove the flash memory write protection, and make it write enabled. To do this, you need to run the stm32flash program. Use the command stm32flash.exe -u COMx . Please read more here, here and here.
- There are a few reports of the STM32 board not working. To verify that it is working, use the blink sketch from the file area, and flash it to your STM32. Verify that the LED (PC13 on the STM32 board, or your TRK-LED if you connected one) blinks.
- If you are able to flash OnStep, and the PC13 LED turns on, and OnStep runs normally, but if you flip the switch to the Run position and power cycle the controller, the LED is off, then it may be bad wiring for the Flash-Run switch, or even a bad switch. To troubleshoot this, try removing the Flash-Run wires and use the original jumpers, and see if that works.
- After you flash OnStep to the STM32, the LED (PC13 on the STM32 board, or your TRK-LED if you connected one) should turn on. The behaviour depends on whether you used TRACK_AUTOSTART ON, or not. If you did, the LED should be flashing. If you did not specify that parameter, the LED should be on continuously. If the tracking LED is off, then OnStep is not running or cannot communicate with the DS3231.
- The STM32 requires the EEPROM on the DS3231 to be working. To test that it is, download the EEPROM Wipe and I2C Scanner sketches from the file area, flash one of them and open the serial monitor to see the output. You should get two devices with addresses 0x57 and 0x68. If you only get one device, or no devices, then your DS3231 could be bad. You can also try the RTC EEPROM Test sketch and see if the read values are the same as the values written. If you use that last sketch, you must wait for it to complete, so the EEPROM is wiped before you can use the ZS-04 module for OnStep again.
- There are rumors that some Blue Pill boards have only 64K of flash. The existence of such boards is unconfirmed so far. If you suspect that your board does not have the 128K of flash required for OnStep, then load the 128.ino sketch from the STM32 Utilities section, and use Serial Monitor at 9600 baud to check the output. Also available is a better flash tester, which is a binary that you download and run from Serial Monitor to test for the existence of 128K flash.
- If you are facing weird problems, you may want to check the temperature at which various components are (STM32, DS3231, stepper drivers). If any component is hot to the touch, or measures above ~ 50C via an IR thermometer or a thermal camera, then there may be a short circuit somewhere. This may cause heating, and component damage, and even fire. Power off the controller, and use a multimeter's continuity test for all pins on the STM32 with their adjacent pins. Visually check the soldering of all components using a magnifier and make sure that there are no solder bridges.
The STM32 is very precise in tracking the apparent motion of the sky. The precision depends on the mechanical design, specifically: steps per degree, which is a combination of the motor (steps per rotation), stepper drivers (microsteps), and overall gear ratio. The higher the gear ratio, the more the precision. However, this is true up to a point, and usually at the expense of slewing speed.
The design parameters for tracking accuracy that best precision is >30.5 steps per second at 0.5us granularity. If you go down to 15.25 steps per second, then it is 1us granularity. With 7.625 steps per second 2us granularity, and so on.
The sky's apparent motion is 15 arc seconds per second. So, the above 'best precision' applies to design having 8,000 StepsPerDegree or more. Ideally, when you are doing What If analysis in the spreadsheet, you want to be at ~ 13,000 or more, and 0.2 arc second or less resolution.
The following are actual examples from STM32 users:
For equatorial mounts:
- Drift in Right Ascension is as low as 0.03"/minute for high StepsPerDegree (~ 16,000 and above, e.g. 28,800 = 200 step motor, 1/16 microsteps, 18:1 transfer gear reduction, and 180:1 Worm wheel). This is beyond what is needed by astrophotography, even for unguided images. In theory, this allows up to 15 minute exposure duration (although see note below).
- RA drift is 0.25"/minute for low StepsPerDegree (~ 1,777). In theory, this is suitable for about 3 minutes of unguided astrophotography, at medium focal length (see note below).
The number of minutes for exposure is theoretical, since it assumes other factors are addressed, e.g. sky glow due to light pollution, periodic error (usually addressed by OnStep's PEC), wind acting on the optical tube, ...etc.
For Alt-Azimuth mounts, drift was 0.12"/minute, in both RA and 0.05"/minute in Declination for a medium steps per degree (~ 12,000). Since Alt-Az mounts are almost always used for visual observation, this is a non-issue.
These changes are in OnStep as of 1.16e and later, released 19th November 2018.
Details of the testing results in pull request 86 and in Howard's measurements.
For Advanced Users
If you are experienced in electronics, you can opt to change a few things in your specific build, including:
- You can use another USB to TTL boards. For example, the FTDI mini USB to TTL board instead of the CP2102 microUSB to TTL board. This means a different cable will be used, mini-USB rather than micro-USB. Some people may simply have them in their spare drawer. Others like the mini-USB cable as it is sturdier than most micro-USB connectors.