Posted on Leave a comment

STM32 Blue Pill ADC Values Too Low in PlatformIO


I kept getting values that were way too low when feeding an ADC on the STM32 Blue Pill 3.3V. I should have been getting values that were around 4000 and instead I was closer to 800.

1) The default ADC in the Arduino library is set to 10-bit resolution. This is what you normally get with analogRead(). To change it, put this in your setup(). Read more about it here:


— This would have been a much easier problem to solve if i had been getting values of around 1000. It turns out that my ADC pin wasn’t getting exactly 3.3V. There was a voltage drop in my system – I’ve got a huge mess currently hooked to the Blue Pill – that knocked it down to around 3V. Normally, calculating bit resolution in decimal is 2^x – 1. So 2^10 – 1 = 1023.

— PlatformIO had nothing to do with the error. I’m new to PlatformIO and never know if my issues are due to the STM32’s specific needs, the Arduino library, or some kind of PlatformIO specific issue. It seems that as long as the hardware is setup correctly and
#include <Arduino.h>
is at the top of the program/sketch/whatever, PlatformIO has no ill effects. The problems I have had aren’t the fault of PlatformIO but from adapting libraries intended for AVR to STM32F1.


Posted on Leave a comment

Genuine STM32 Blue Pill vs CS32F103C8T6 Clone

I’m troubleshooting a synth design and looking at the data on the SPI bus.  I have 10 STM32 clones (CS32F103C8T6) of the STM32F103C8T6 around and decided to try them out since they have the correct USB resistor.

I was getting the following error in PlatformIO.

Warn : UNEXPECTED idcode: 0x2ba01477

To get the clone to work, I had to change stm32f1x.cfg which I found in C:\Users\YOUR_WINDOWS_USER\.platformio\packages\tool-openocd\scripts\target\

I changed set _CPUTAPID 0x1ba01477 to set _CPUTAPID 0x2ba01477.  If I need to go back to the real Blue Pill, I’ll have to change this back again.

I started with the clone and then switched to the real one.  The scope shows some interesting results.  This is data from the SPI bus.  The circuit is 100% identical in both cases.

CS32F103C8T6 Clone SPI Data

Genuine STM32F103C8T6 SPI Data

I’m not sure if the clone just can’t handle the speed or if there’s some kind capacitance slowing it down.  Regardless, the SPI output of the clone is so bad that I’ll have to check the timing diagrams for my SPI-receiving chip.  I don’t have this problem with the genuine STM32.

If anyone knows a solution to get the clone to behave, please tell me.  Otherwise, I’ll have to view these Chinese clones as WAY too expensive once time is factored in.

Posted on Leave a comment

bCNC Safe Z

bCNC comes with a feature to move the spindle up to a safe z height.  If you right-click on the “Safe Z” button it looks like this.

G90 G0Z(safe)

The default is 3.  In my case, it was 3 millimeters.  I wouldn’t call that safe at all.  I use clamps that are much thicker/taller than 3mm and most have bolts that stick up closer to 75mm.  I had big trouble figuring how to change the definition of Z(safe).  More importantly, I didn’t like the G90 in the code above.  G90 relies on my working coordinate system.  In my mind, the Safe Z should take the spindle as far up as possible.  This happens to be an absolute positioning thing.  I’ll back off 5mm just to be safe.

I entered this code in the Safe Z window

G53 G0 Z-5

It should look like this.

Now when I press Z, no matter how stupidly I’ve set the working Z0 coordinate, I know the spindle will raise up to it’s maximum (almost).

  • I use a homing cycle.
  • I’m using mm for my units.

Adjust accordingly.

Posted on Leave a comment

Use Python To List All Files In A Directory

I needed a lengthy list of files for a massive 3D printing project I’m starting and I didn’t want to have to copy and paste each file one at a time.  Brandon’s Rule #232:  If you are faced with a 2 minute task that bores the hell out of you, invest 15 minutes into automating it.

Everyone should have Sublime Text installed on their machine and have just enough competence in Python to Google the living crap out of any problem that arises.

This Python code simply lists all the files in the directory containing this file.

from os import listdir
from os.path import isfile, join

mypath = '.'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

for x in range(len(onlyfiles)):
Posted on Leave a comment

My Saga With PLCs – Part1

I didn’t learn PLCs in school.  My gig was Engineering Physics and if the homework didn’t use Calculus to think about the atomic level, I didn’t learn it.  Granted, I didn’t learn to use Calculus to think about the atomic level, either, but that’s another rant.  So, I’m learning PLCs.  Here are some thoughts for others entering this domain.

  1. There must be no money in PLCs anymore. (joke)  Allen and Bradley made some truly cutting edge software at the tail end of the Reagan administration and then halted all progress.  This, in and of itself, ain’t a big deal.  The big deal is that 3 decades of progress have radically reduced the time wasted on clunky software.  If you use a modern IDE to program anything, you’ll be frustrated by RSLogix.  If you use Notepad to program anything, you’ll still be frustrated by RSLogix.
  2. The story goes that Ladder Logic was designed to be simple enough for non-programmers to use it.  I’m not a “programmer”, but I’ve done a fair chunk of it.  The need for visualization adds an extra layer to the mix.  Instead of remembering the XIC is “examine if closed”, we also have to remember what the schematic symbol looks like and where we can find it and how to get it into our schematic.  It would have just been much more efficient to type “XIC”.   A GUI needs to hit a critical mass of usefulness in order to displace text-based tools.  The GUI in Notepad++ is pretty straight-forward.  We all know how to manipulate text.  Finding the menu with XOR in it is not so clear.  Hopefully, getting adapted to RSLogix won’t be so bad after I force myself through the blatant inefficiency.
  3. I started down the PLC thing months ago.  It’s taken me months of working here and there to get a working hardware setup.  (4 switches, 4 lights, and connected to the PC.)  I spent nearly a week working on the ethernet connection.  I found an authority online who said, “Absolutely do not use Ethernet! on a MicroLogix PL1100”.  I bit the bullet and bought the $25 Serial cable. It worked immediately.  It’s my understanding that the ethernet on the MicroLogix1100 is of the primitive variety….whatever that means.  If a person has to use ethernet, the recommendation seems to be to use a “crossover cable” ($6 on Ebay) to allow plugging it straight into a computer somehow.
  4. The $25 Serial cable was a total waste of money.  It works, but I have $2 FTDI chips all over the place.  (FTDI converts USB to serial and vice versa for use as COM ports in Windows or equivalent.)  I only needed power and 2 wires.   The MicroLogix PL1100 uses a fairly goofy connector that I don’t have.  So, I basically paid $25 for a connector.  Oh well.
  5. The $25 Serial cable was actually a knockoff from Amazon.  The Allen and Bradley cable is a whopping $50.  To me, this is a $2 board from Ebay.   You see ridiculous pricing all over the place in PLC land.  The most notable example is the PLC Trainer.  They often come with 4 switches and 4 lights and cost $150+.  Since no one with sense is driving 100kW systems while TRAINING, there’s no point for these to be high-power, industrial powerhouses.  Switches rated for 125V on Digikey start at $1 a pop.  The lighting is even more grotesque.  Industrial-grade indicator lights are expensive because they are built like tanks.  As cool as tanks are, you don’t want a tank in your basement.  An LED and a resistor, believe it or not, is capable of acting as a perfectly viable indicator-via-photon tool.  This does lead to opportunity, but I have to admit I get nervous when prices are obscenely inflated and no one seems to question it.  I’m sure questioning it.  Look for an Electron Injection PLC trainer soon.
  6. It seems obvious to me to use an LED and resistor for indicator lights.  They’ve been the absolute standard since they were invented.  The LED is the default tool to use for such a job.  I can’t see how any engineer or technician learning PLCs wouldn’t immediately think to grab a 680 ohm resistor and a red LED out of the cabinet.  A person has to go through a minefield of rationalizations to arrive at the hyper-expensive alternative for the same reason you don’t book a flight in a B2 bomber to go Cancun.
  7. The worst part about learning PLCs this far is the lack of projects.  It’s well understood that the best way to learn Arduino (or anything in life) is to have a big problem you plan to solve with it and jump in.  Let’s say we want a robot that can plant seeds.  When you finish such a project, you end up with a seed planter.  Cool!  The problem with PLCs is they cost about 100x what microcontrollers generally cost.  (Give or take.)  It doesn’t make sense to jump into such “hobbyist” projects with industrial tools.  So what are you supposed to do?  Start your own beer factory in your basement?  (Don’t tempt me!)  Even then, it’s not exactly clear why I’d use a PLC when I could just program a $5 microcontroller.  Maybe that will be revealed down the road.
  8. I learned pretty quickly that the emulator for the Allen and Bradley PLCs was omitting quite a bit of the “PLC Experience”.   Here’s a metaphor.  Your can emulate changing the oil in your car, but it will never drip oil in your face.  There’s some nastiness that needs to be addressed with the real thing that gets lost in the emulators even though I REALLY wanted the emulator to work out.
  9. Get ready for some work.  I decided that I hated the user interface.  Again, this is common when doing anything new.  I was hoping for a shortcut by jumping straight into the code.  WRONG!  Here’s the code for my tiny little “program”.

     BST XIC I:0.0/0 NXB XIC I:0.0/1 BND BST XIC I:0.0/2 NXB XIC I:0.0/3 BND OTE O:0.0/1 

    Look at the end first.  We have an OTE to energize the output at 0.0/1 whenever the rest is true.

    As for the rest, I’ll show you the C version.

    if (   (BIT0  OR BIT1)  AND  (BIT2 OR BIT3)  ) {

           Output  = 1;


    I’m reminded of how lucky I am to have the C language.  This is only an illustration to show that if a person had some hardcore coding to do on a PLC, the GUI is where it’s at.  The code behind PLCs reminds me a bit of Assembly and no one wants to go there. 


My earliest days with PLCs have been less than ideal.  It’s not PLCs fault that I can’t economically use them for stupid, little projects.  That takes most of the fun away.  However, I’ve made myself a list of assignments…..easy stuff like “Push a momentary switch and turn an LED on for 8 seconds”.  By stacking up enough of these little hurdles, I think I can begin to get a feel for this thing.

PLCs are a radically different culture than I’ve experienced with programming microcontrollers or playing with Python or even HTML.  The terminology is different.   They often make things much more complicated than they need to be.  The above C vs PLC code above is a great illustration of that.    They will often say things that are either incorrect or could easily be made to be incorrect.  For example, the whole idea of XIC is “examine if closed”.  In real life, they are asking if an input is at a HIGH level and is so, do something else.    Well, I can create either HIGH or LOW levels with either a closed or open switch using pullup resistors on a microcontroller.   Why do they assume the switch is closed?  It’s okay that they have, but it would have been nice to have this explained to me.  In my mind, reacting to HIGH and LOW conditions is just as simple and not so presumptuous.  There are REALLY good reasons why you may want to utilize active HIGH or active LOW (see the NC vs NO debate with CNC machines limit switches, for example).  This is just one example.  Maybe I’m making it too complicated…..

Making these simple generally makes them more complicated.  PLCs seem to fall into this category over and over for me.  Again, when I finally adapt to Ronald Reagan’s RSLogix, I suspect things will get easier.  The quirks are all over the place.  With that said, if a person was diving into bare metal C on a microcontroller, they would probably see even more quirks.  Too bad.  I’m generally used to those so they don’t count.  (smilie)