Category Archives: Raspberry PI Hat’s

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.


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.


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.

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.

High Side Current Sensor

I decided to Change the 12 x PWM Hat to use High Side Current Sensors. Below is the schematics of the new sensors. This means I measure current out on each PWM signal.

Using high side sensors actually simplified the routing so it was a straight forward change. The reason I started with low side sensors is because that is straight forward for DRV8313 and more common for 3-phase. It would have been ok for Stepper and DC motors, but it made it difficult to measure current if you used a single PWM channel stand-alone.

12 x PWM Hat

 This 12 x PWM Hat have 12 x low side current sensors. The PWM driver have a separate PSU, DRV8313 is rated 60V and INA194 is rated -16 to 80V so they are not that easy to break giving the Hat a very decent protection level. A separate TVS and capacitor bank on motor PSU will in addition suppress unexpected spikes + there will be larger protection on the PSU itself. I feel quite comfortable with this design, but testing will show.

Connecting the current sensors low side is not the most optional for individual PWM signals, but it is ok for 3-Phase and Stepper Motors where I wanted them the most. The challenge with PWM is that current mostly leave high side out and get connected to ground, meaning we never pass a current sensor on low side. But – again – I do have a SW trick that I will test. Adding these low side was most straight forward due to DRV8313. But, I will actually evaluate if I should move them high side. This would actually simplify the PCB – I think.

I am looking forward to work with this one due to the advanced functionality you get with current sensors – and if anyone wonder the first project is actually my 3D printer. I will write a separate article about that later.

3-Phase/Stepper/RPM Hat

This Hat was actually a challenge to route, but I managed it at the 4th attempt. I am quite happy with the result as well.

  • Raspberry PI Hat Format
  • STM32F405RG MCU, 32bit ARM M4, 168Mhz, 1Mb Flash, 196Kb SRAM.
  • 42Mbps backbone network. ca 30Mbps With Raspberry PI.
  • CAN Network
  • USB
  • Separate PSU for PWM
  • 12 x separate PWM signals, ca 1A each.
  • 3 x Stepper Motors 2,5A
  • 4 x 3-Phase Motors 2.5A
  • 6 x DC Motors.
  • 24V design capable to support 48V with some cap/diode changes.
  • 24 connector terminal block with 12 signals and 12 ground connections.
  • 12 separate current sensors.
  • 12 separate Half H-Bridges allowing a very flexible usage.
  • Based on DRV8313 3 x Half H-Bridge Driver.

I have many designs, but the flexibility of this one is in a category of itself.

  • Gimball controller
  • CNC/3D Printer controller
  • Running DC Motors/PWM signal up to 10A by combining ports.

I must also admit that it feels good to be finish with this after so many failures on routing this very design. I have done many weighted compromises here, so I will need the MCU on this one, but it will be fun!

My motivation to do this one is that it is a key component in a very special control system where it is possible to actually earn some money. But, well – I can’t tell every secret in here 🙂

Current Sensor Math

I currently plan using INA194 that have a 50x gain. As our max voltage in is 3.3V this means the max voltage over the shunt need to be 3.3/50= 66mV. Given a max current of 2.5A that gives a Shunt resistor R = V/A = 0.066/2.5 = 0,0264 R or 25mOhm.

Now – 2.5A over 25mOhm is 0,156W which is fine since the 1206 resistor is 250mW.

If I use 50mA that gives 0,00125V (1,25mV) into INA194. And with a gain of 50x that is 62,5mV into the MCU. This is where we get into trouble because INA194 starts being inaccurate below 50mV in. I am not at all sure I actually can measure these low currents, but I do have one SW trick to help me.

We are NOT measuring a constant current, we are measuring a PWM pulse current, and the current we will see is 2.5A for 1mS and 0A for 999mS (just an example) due to the nature of the PWM. In software that becomes 2,5A / 1000 = 0,0025mA in average over a sec. So, if I take advantage of the 2,5Msps capability on the STM32 M4 I should be more than ok. I just need to make sure my sample frequency is at least 2 x the PWM frequency, but the higher sample frequency the better in this case.

Yet another trick is that I know then the Pulse is On/Off, and since we have raw data (no electronic filtering) I can eliminate noise then the pulse is off – as I simply know there should not be anything there. This is more difficult to do than I write here, but I will dig into it.

I used 2.5A as an example, but pulse current is V/R and can be higher that the rated max. This is why some MOSFET’s are rated to 160A with a pulse current to 400A etc. If we have motors with low inductive R we reduce PWM duty to maintain 2.5A in average, so I might actually need to cut down even further on the Shunt to avoid clipping on pulse currents, but I will not worry about that for now. I want to experiment with actual values because you can only cover so much in theory. Noise etc also add to the picture and is not easy to predict or model accurately.

The irony is that I removed passive filter components because of lack of space, and those filters would have prevented us to see the pulse currents. Now – since I have raw noise in I can actually use SW and see far more accurately than I would have done with analogue electronics. This is an excellent example where you need a good interaction between a HW and SW engineer.