0

I am having difficulties when printing any type of variable using the Serial.print(ln) command.

The Serial Monitor becomes inactive after printing for approximately 7 - 15 times.

I currently have the STM32 Blue Pill Board, running Mac OS Mojave and Arduino IDE v 1.8.9

void setup() {
  Serial.begin(9600);
  pinMode(PC13, OUTPUT);

}

void loop() {
  float raw = analogRead(PA0);
  float voltage = (raw * 3.3) / 4096;
  Serial.println(voltage);
  delay(500);
}
1
  • isolate the area of the error .... start by changing the print command to Serial.println("voltage"); .... it will print the word "voltage" .... just let it run to see if it also hangs up
    – jsotola
    Aug 12 '19 at 14:45
1

Have you made sure to modify the board before using it?

This page mentions at least one resistor that you have to replace for the USB to "work".

The USB standard requires a 1.5 kΩ pullup resistor on D+, but this board is known to have a wrong value (R10 on the board). It ships with either a 10 kΩ resistor or a 4.7 kΩ resistor, but it should be replaced with a 1.5 kΩ resistor, or put an appropriate resistor value (e.g 1.8 kΩ) in between PA12 and 3.3V.

It is also true that some PCs are tolerant of incorrect value so, before you change the resistance, you can try if it works in your case.

I have had issues with bluepill boards myself in the past, but never had an issue like yours after fixing the resistor. (before that, I couldn't even flash the bootloader)

Try printing other things with the board, and see if there's any pattern to it cutting out. e.g. a string over and over again, or the same float over and over again.

Try different baudrates, or even SoftwareSerial to another board.

0

Printing a float should work (at least with an Arduino), but to test if it cannot handle it correctly, I suggest you convert the float to a string (e.g., with the C function ftoa, and print the characters one by one or as a string.

Since this C function is not available on Arduino, use the function implementation of this article, credits to Martin Waller:

char *ftoa(char *buffer, double d, int precision) {

    long wholePart = (long) d;

    // Deposit the whole part of the number.

    itoa(wholePart,buffer,10);

    // Now work on the faction if we need one.

    if (precision > 0) {

        // We do, so locate the end of the string and insert
        // a decimal point.

        char *endOfString = buffer;
        while (*endOfString != '\0') endOfString++;
        *endOfString++ = '.';

        // Now work on the fraction, be sure to turn any negative
        // values positive.

        if (d < 0) {
            d *= -1;
            wholePart *= -1;
        }

        double fraction = d - wholePart;
        while (precision > 0) {

            // Multipleby ten and pull out the digit.

            fraction *= 10;
            wholePart = (long) fraction;
            *endOfString++ = '0' + wholePart;

            // Update the fraction and move on to the
            // next digit.

            fraction -= wholePart;
            precision--;
        }

        // Terminate the string.

        *endOfString = '\0';
    }

   return buffer;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.