[SOLVED] digitalWrite() HIGH means LOW on blue pill? (no)

tbyte
Thu Jun 07, 2018 1:38 am
I have noticed that when I set the PC13 pin to HIGH the LED goes off and when I set it to LOW it goes on ! I tested it with a multimeter and when LOW it’s 3.3V on the pin , when HIGH it’s 0V. I’ve tested it with 1 and 0 instead of HIGH and LOW – same thing. Tested it on pin PC14 … same thing happens. Am I doing something wrong or is it a bug ?

#ifdef SERIAL_USB
#define SPRN Serial1.print
#define SPLN Serial1.println
#define SERB Serial1.begin
#else
#define SPRN Serial.print
#define SPLN Serial.println
#define SERB Serial.begin
#endif

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin PB1 as an output.
pinMode(PC13, OUTPUT);
SERB(115200);
SPLN("Setup done.");
}

// the loop function runs over and over again forever
void loop() {
SPLN("LIGHT ON");
digitalWrite(PC13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(3000);
SPLN("LIGHT OFF");
digitalWrite(PC13, LOW); // turn the LED off by making the voltage LOW
delay(3000);
}


fpiSTM
Thu Jun 07, 2018 4:51 am
HIGH or LOW doesn’t means 3.3v or 0v.
It depends if the LED is PULL up or down to VCC or GND.

Pito
Thu Jun 07, 2018 5:36 am
HIGH or LOW doesn’t means 3.3v or 0v.
“High” on an output pin is always 3.3V, and “Low” is always 0.0V with CMOS 3.3V logic (approximately, it depends on the load).

An LED lights ON with “High” or “Low” – that is based on how the LED is wired:
pin -- resistor -- cathode |<| anode to Vcc LED ON = LOW
pin -- resistor -- anode |>| cathode to Gnd LED ON = HIGH


tbyte
Thu Jun 07, 2018 9:42 am
The LED is the built in one. So the pin is not a on/off switch but 3.3V(HIGH)/GND(LOW) ? Which means either the board is wired the wrong way or the “blue pill” is done the wrong way by default (it has the wrong R10 either) :) Can someone test on their cheap blue pill and see if it is the case ? It can be a WIKI addition like “Some blue pill LEDs are wired the wrong way (don’t post another bug report) ” :)
Oh and the code is from the STM32’s examples which means this part is kind of misleading because it make it sound like it’s just an on/off switch wired to VCC(3.3V):

void loop() {
digitalWrite(PB1, HIGH); // turn the LED on (HIGH is the voltage level) <- misleading part ?
delay(1000); // wait for a second
digitalWrite(PB1, LOW); // turn the LED off by making the voltage LOW <- misleading part ?
delay(1000); // wait for a second
}


stevestrong
Thu Jun 07, 2018 11:15 am
If you carefully read the example, it is written for the maple mini, which has the LED on PB1 and is turned on when HIGH is applied to the LED.
Each board can have LED connected in different ways, as Pito mentioned.
On the blue pill, the LED is turned on when LOW is applied.
This is not a bug.

tbyte
Thu Jun 07, 2018 11:42 am
[stevestrong – Thu Jun 07, 2018 11:15 am] –
If you carefully read the example, it is written for the maple mini, which has the LED on PB1 and is turned on when HIGH is applied to the LED.
Each board can have LED connected in different ways, as Pito mentioned.
On the blue pill, the LED is turned on when LOW is applied.
This is not a bug.

That is why I said this:
It can be a WIKI addition like “Some blue pill LEDs are wired the wrong way (don’t post another bug report) “

PS: Btw is it confirmed that it is that way on all Bluepills ? If so it can be added to the Bluepill section of the WiKi as something normal.


edogaldo
Thu Jun 07, 2018 11:47 am
Blue pill wiki, it’s explicitly written:

Blue pill led.JPG
Blue pill led.JPG (19.57 KiB) Viewed 749 times

tbyte
Thu Jun 07, 2018 11:50 am
Oh sh … sorry for the noise :(

Pito
Thu Jun 07, 2018 11:52 am
“Some blue pill LEDs are wired the wrong way (don’t post another bug report)
There is only one BluePill board and it is wired ok.
As I wrote above, different boards may have the LED(s) wired in different way(s).
There are basically 2 ways how to wire an LED (see above).
Thus, with different boards, you have always to double-check, whether LOW or HIGH level lights the LED ON.

A good engineering practice with MCUs is to wire an LED such it lights ON when the pin level is LOW. It is because the pin’s sink current capability is usually higher than its source current capability (it can sink much more current than source).


tbyte
Thu Jun 07, 2018 11:58 am
edogaldo showed me how blind I am :) . Now I do this:

#ifdef ARDUINO_GENERIC_STM32F103C
#define SPRN Serial1.print
#define SPLN Serial1.println
#define SERB Serial1.begin
#define MYLED PC13
#define LEDON LOW
#define LEDOFF HIGH
#else
#define SPRN Serial.print
#define SPLN Serial.println
#define SERB Serial.begin
#define MYLED 13
#define LEDON HIGH
#define LEDOFF LOW
#endif


Slammer
Thu Jun 07, 2018 4:38 pm
The driving of a led or a load generally, by 1 (sourcing) or by 0 (sinking) is not an oddness of the designer, it is very common for MCU’s gpio pins to have different current capability for sourcing and sinking, generally the sinking current is greater.
FIY, STM32 chips have symmetrical current capability, about +/-8mA (nom.), but this is not the rule, many other chips, for example AT89 has sinking current 1.6mA max and sourcing only -800uA max.

Leave a Reply

Your email address will not be published. Required fields are marked *