Unipolar Stepper Logic

This is the diagram of an Unipolar Stepper Motor with the 5-wire connector at right. The most used Unipolar stepper is the 28BYJ-48-5V pictured below and this is the target of my 7x5WStepper Hat.

This is a high quality/low cost stepper available for < 2.-USD. I purchased one for test and was quite take by its performance. It is silent and its 64 steps/turn is better than you expect due to the build in gear that also makes it strong. It is a bit slow, but all-in-all this is a really excellent stepper motor which is why I decided it needed it’s own control Hat.

Running this is dead simple as you apply 4 steps in sequence.

  1. 0+1
  2. 1+2
  3. 2+3
  4. 3+0

Reversing the sequence will run the Stepper in the opposite direction. The algorithm have some similarities to trapsoidal on a 3-phase, but the difference is that 1 step moves the motor one step due to the strong cogging in these motors.

To assist us I will create 2 C++ classes. One is hGPIO that wire up the pins, and the second is hStepper that takes the pins and steps. So with an array of 7 x hStepper I should be able to run 7 steppers simultaneously.

What is interesting with this is that one Hat control 7 steppers, 2 Hat’s 14 steppers and so on. And as the Steppers are low cost it is realistic to build some advanced robotic Experiments.



All MCU vendors will deliver some C code they call “HAL”, while I call that Low Level Drivers (or BSP – Board Support Package) and implement my own HAL (Hardware Abstraction Layer). A proper HAL need to abstract from hardware and secure portability of code. But, most important is that I want to write source code in C++11.

StarUML cost a few bucks, but I like it and it’s the only alternative I have found to do decent UML class diagrams. The class diagrams makes it easier to maintain overview and as such they improve quality of software architecture and documentation + they don’t take much time to draw.

I started learning C back in 1983 and coming from languages like Jovial, Fortran, Basic and Pascal it felt like heaven in comparison. Later in 1994 I learned OMT and C++ that was a good fit and I have been a C++ fan ever since. Yes I use C# and Java as well, but only if I can’t avoid it.

This blog have so far been more about electronics than source code, but I have stated a few times already that this will change. The starting point is 3 libraries that will form the basis of all we do.

The first is HAL – Hardware Abstraction Layer. A library that systematically encapsulate hardware in a functional, abstracted way.

The second library is EFC – Embedded Foundation Classes, a library of tightly written C++ targeting embedded MCU’s. This library was started years ago and have been mentioned before because as soon as I move on functionality I also need building blocks.

I will return to the 3rd library/tool later.

7x 5-Wire Stepper Hat

This show the 7 x 5-Wire stepper with the intended connectors and an example of the stepper connected. I have 2 errors on this Hat – firstly I have the +V on the wrong side, so I have to turn the connectors around. I will just use straight connectors for testing. The second is that the PWR connector on the right, bottom corner is to close to the Led. Other than that this seems to be working well so far.The picture below is with straight connectors that I need to use for now.

The jumper in the middle right allow me to connect Stepper Power to USB, but it really is recommended with a separate PSU.

This tester made a big difference. I soldered off what I believed was a failing MCU and tested it to be ok, then soldered it back on and it worked. Turned out I must have had a bad soldering. The tester is the best investment I have done because it saves me wondering if a MCU works or not.I am also getting aware that I have a lot of cold solderings causing problems so I will see if I can start using the oven more.


MCU Tester

My STM32 Tester finally arrived, and 12 rejected MCU’s which is ca 40% of all STM32F405’s I had is the result. The sent me a different tester than I ordered, but it did the trick. One MCU broke under testing as well.

10 x STM32F405RG cost va 45.- USD from China and ca 90.- USD from Farnell in Norway. With 40% loss and the unknown of factory rejects I can as well just order from Farnell.

32 x IO Hat

This is the last version of the 32 x IO Hat. I just modified it to use the faster SPI towards RPI bus. The Power connector on the right side is for Servo’s. It enables a different voltage than 3.3V supplied from a separate PSU Hat. The jumper need to be set for 3.3V to be connected to IO. Every line has TVS protection, so this is a decently safe board.

This board basically export 32 pins with TVS protection and add V+ and GND making it ideal for Servo, Sensors or just a plain GPIO Hat.

  • 22 Timer based PWM channels
  • 32 SW driven PWM channels
  • 15 analogue input channels
  • 2 analogue output channels
  • 32 GPIO of which 17 is 5V tolerant.
  • UART/CAN/SPI/I2C ports
  • High speed SPI backbone network
  • CAN Network
  • USB
  • SWD

Wifi Hat Failure

This ESP32 Hat  work perfect on IO, but as I switch on Wifi it short-cut and reboot. I might have encountered my first bad ESP32 module, but I am not sure yet. I need to assemble another unit and see if this works as this is the first time I assemble this board.

This is an ESP based Hat in Raspberry PI Format. This is basically an experiment as I want to test SPI and CAN on ESP32. The design only serve as a low cost alternative to using a Raspberry PI as this has both Wifi, Bluetooth, CAN, RS485, UART TTL, USB as well as 5 Sensor ports.

To be continued.

Stencil Printer

To effectively have a small production line you need to have 3 pieces of equipment/processes.

First step is using a stencil to apply solder paste. this can be done with a stencil alone as a mask on top of a PCB, but a better way is to use a 200.- USD stencil printer. This pic show a low cost one for ca 150.- USD.

Second step is to use the pick and place machine to add components.

The last step is to put this into an automated reflow owen. I actually have one of these, but have not used it much because I solder very fast with a heat fan.

I have 2 reasons for wanting to start using the stencil. It is faster and easier to add correct solder paste, and by using the owen I avoid the number of cold soldering problems I have.

I just assembled a wifi board and sat hours fixing 2 cold soldering – soldering that look perfectly ok through a microscope, but actually is not connected. These problems take time and the first step in improving is to actually use stencils and the owen while I ramp up for a full production line.

The drawback is that a PCB that usually cost 5.- USD now will cost 50.- due to the cost and P&P for a stencil with frame.

Pick & Place Machine

One of the challenges I face is that I would like to start producing electronics in volumes, but assembling at an factory is expensive. It easily add 50-100.- USD to unit cost at low volumes and I don’t have the financial backing to go high volumes.

One alternative is to use an Asian factory, rather than the expensive Norwegian ones. They differ a lot on low volumes. The other alternative is to invest in equipment for low volumes yourself. Acceptable Pick & Place machines and Stencil Printer solutions exist from ca 4000.- USD. It is specially 2 machines that caught my attention: CHMT36VA and TVM802A. These are close to identical in functionality and price.


The pictures above is CHM-T36VA and CHM-T48VB. The difference is that the later have PC embedded (Linux) and 2 x as many feeders (58) and cost ca 5000.- USD compared to 3200.- for the smaller one. CHM-T36VA need an external PC. CHM-T48VB really is a good deal.

The key with these machines are that they have cameras capable to calibrate components giving them a much better accuracy that previous low end solutions. Their limit is that they only have 2 heads and 27-29 component feeders. So for more advanced electronics you will need to swap reels and nozle’s etc. They are also decently noisy and somewhat slow, but they will do the trick.

My reality is that I spend a huge amount of hours soldering proto-types and these machines can do that much faster and more reliable where I only solder on hole through and special components myself. And once the machine is set up I can easily produce a decent number of boards close to automatically. More important is that it will enable me to work with BGA style components.

Another cost is components. I basically need to invest in a decent stock of reels, meaning I will need a much larger stock of components. IC’s are different as the machines can pick them from trays and you can buy lower volumes. But, it is doable.

The design of a low cost Pick & Place machines consist of several main components you need to evaluate.

  • The CNC frame with various axis and tools. This needs to be low noise as well as very accurate. The size/speed also decide prod volume.
  • The feeders enabling the machinery to pick up components. You want as many of these as possible and a combination of Reel, Tray and Tube-feeders.
  • Calibration cameras. Early machines did not have these and did decently well, but with camera’s you get automatic calibration on Components and avoid errors that was common in early machines.
  • Placement heads. The smaller machines have 2 heads, but you really could need more as different nozzles are needed for different components.
  • Control system on these are a combination of sensors, stepper motor drivers, camera inputs and usually a PC level computer to add Logic.

The challenge is that increasing number of feeders and heads also increase investment cost. I think 3.200,- USD is as far as I am able to go for now and I really like CHM-T36VA + I have plenty of PC’s and screens around.

New 3D Printer

I do at present have 8 PCB’s + 3 designs I have not ordered yet in my modular control system, so I have a far to large backlog of electronics that need testing and code. The difference with my last 12 x PWM Hat is however that it target a project – my upgraded 3D printer.

I purchased a Prusa i3 which basically is a clone of a generation 1 design and I want to use my modular control system on this one. I actually have 3 Arduino based control systems of which 2 used a fake FTDI chip that caused problems. But, I want to add features not available in the old control system.

Multi-material support means more than one extruder. This is mostly a mechanical re-design, but I need extra Stepper motor controller and logic to handle it.

Automatic calibration. Those of you who have used an early version of a 3D printer know how much work that goes into calibrating and ensuring that the printer is correctly configurated. This can be done automated by sensing the tilting of the plate using a “Z-Sensor”.

Torque based end-stops means that I do not use end-stop sensors, but sense current increase as the CNC machinery hit mechanical stoppers. This is goodbye to a lot of problems and wiring.

In the illustration above I assume I will use 5 modules to make a modular control system. One Wifi module that can be Raspberry PI or ESP32 based, a XPortHub to access USB, MMC and HMI. MMC can be used to spool jobs. A 12 X PWM used as 4xStepper to control X,Y and 2xZ Steppers. A 2nd 12 x PWM to control 2 x Extruder feeders and 3 x temperature heaters for heatbed and extruders, and finally a 32 x IO to get temperature sensors and a Z-distance sensor. This is just an early illustration.

The Z-distance sensor I want to use is a small laser that can sense the distance from Extruder to the plate. This can during calibration sense the tilting of the base plate and adjust G-Code parameters so it get correct.

This is an excellent test bed for my modular control system. It is so many excellent All-In-One systems for 3D Printers and my favorite is the new MK3 from Josef Prusa so we could have done this much easier. But, I need a test bed and this is an excellent start to drive development.