Some Information on the BluePill (STM32F103C)
Below are some observations one might encounter when working with the STM32F103C
as a micronctoller in conjunction with the Arduino STM32 library. A good secondary source for documentation s Leaflabs Maple.
Some of these details here are specific to a certain STM32F103C model, such as the LED pin. The following information should be used to guide, rather than to follow directly.
Electrical
- The onboard LED is on pin
PC13
. Lights up when the output for this pin isLOW
. - Has an onboard 3.3V voltage regulator. This drops input power from the 5V pin and from USB to 3.3V
- Operates on 3.3V, rather than 5V on an Arduino.
- The Blue Pill's 5V regulator is not the best in the world, especially when sourced from cheap Chinese sites, so don't expect the Blue Pill to be able to run a lot of LEDs or Servos. The stock 3.3V regulator can handle up to 300mA.
Pinout
More Pin Info
- Non-5V tolerant pins will be damanged when receiving a voltage near 5V.
- Pins
PA9
andPA10
are used to interface with the USB to TTL adapter. However, you should not need to do this. (e=read the seciton on Uploading) - Pins
PC13
,PC14
,PC15
are to be used if crucially needed. These pins cannot sink or source a lot of current. Damage to these pins could result in damage to the internal oscillators and the real time clock. - Pin
PB2
is useable, but placed in an awkward position. - Pins marked
ADC
supportanalogRead()
andpinMode(PIN, INPUT_ANALOG)
. These pins are 12-bit, so the range is0
to4096
. - Pins can be 'softly' pulled up to 3.3V or down to ground with the pin modes
INPUT_PULLUP
orINPUT_PULLDOWN
with the use of internal 22k resistors. Example: IfINPUT_PULLUP
is used, the state will be high unless an external device is specifically pulling the pin down to ground. The “gentle” pull up will not affect the state of the input.- This is best used for digital inputs.
INPUT
is the same asINPUT_FLOATING
- To enable PWM output on
PA11
, we must give up serial over USB, since one of the USB data signals is mapped to this pin. To do this, comment out all the contents invoid USBSerial::begin(void)
inusb_serial.cpp
located in.platformio\packages\framework-arduinoststm32\STM32F1\cores\maple
PA12
is HIGH when the Blue Pill is turned on (before any code execution).
Software
- With PlatformIO and the Arduino STM32 library, the Blue Pill can be used very similary to an Arduino. Follow these instructions to get up and running.
- One can use the traditional pinModes by using itegers such as
pinMode(13, OUTPUT)
, but it is recommended to use enumspinMode(PA3, OUTPUT)
. - It is possible to iterate over a set of pins by iterating through the enums:
enum {
PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13,PA14,PA15,
PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13,PB14,PB15,
PC13, PC14,PC15
};
Uploading
- To have the system be able to detect the serial device over USB, download the Arduino STM32 repo, navigate to
\drivers\win
and execute theinstall_drivers.bat
script. - Once done, the Blue Pill should show up in Device Manager as a Maple Serial device.
- Ensure that
platformio.ini
has the appropriate settings (Load with your COM port):
[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = arduino
upload_port = COM8
upload_protocol = dfu
- Upload the sketch from PlatformIO. The IDE will attempt to set the Blue Pill into DFU mode to upload the sketch. If this fails, or it is stuck, simply hit the
RESET
button on the Blue Pill when PlatformIO is searching for DFU devices.- Becuase USB uploading does not require moving
BOOT0
to1
, resetting the Blue Pill will only provide a short window to allow the IDE to upload the sketch.
- Becuase USB uploading does not require moving
Using Motors and Servos Simultaneously
This information is applicable for when the microcontroller is used to switch motors and to control servos using the default libraries.
When servo.attach()
is called on a certain pin, the hardware timer responsible for creating the PWM signal is set to a certain period/frequency. On the Blue Pill, this period/frequency is synchronized across all pins that the timer is responsible for. So if you have a motor running on the same timer as a servo but another motor running on a different motor, the motor speeds will differ because their PWM frequencies will differ.
One fix is to coordinate pinouts such that all motors run on their own timer, but usually we can't afford this luxury so we turn towards a different solution, by synchronizing all of the timers to be the same.
void timerSetup() {
for (int i = 1; i <= 4; i++) {
HardwareTimer timer(i);
timer.pause();
timer.setPeriod(20000);
timer.refresh();
timer.resume();
}
}
A period of 20 milliseconds is chosen because this is the period that servo.attach()
sets the pins to.
Motors and Sensing
Motors add noise to the system, and this brings about bad news if the pins are floating.
When using limit switches, since the states can be interpreted digitally, the appropriate pinmode should be INPUT_PULLDOWN
.
When using analog inputs, it is desirable to be stopped when reading to minimize noise. Other electrical and mechanical considerations can also reduce noise.