I would like to know where I can see the output when I use the command printf
. I am programming using the Arduino interface.
Update
I'll add a few lines of code for better understanding:
#include <stdio.h>
int main(void)
{
printf("Hello World");
return 0;
}
3 Answers 3
If you're working with the standard Arduino environment and libraries then printf()
won't display anything anywhere. The reason is that microcontrollers could be connected to any number of different output/communication devices in any way. stdio
would have no idea how or where to send the data.
The most common approach to outputting text from an Arduino (particularly during development) is to send it to the primary UART port using Serial.begin()
and Serial.print()
. If the Arduino is connected to your computer via USB then the Arduino IDE's serial monitor should display the result.
If you really want to use printf()
though then it is possible to make it work (or implement similar functionality). A full explanation is on this page:
In setup()
, start serial and choose your speed:
Serial.begin(9600);
Wherever you want to write to serial:
Serial.write("write this");
Wherever you want a newline:
Serial.println();
example
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.write("Hello World");
}
void loop() {
// put your main code here, to run repeatedly:
}
-
my question is actually where am I printing if I don't define
Serial
invoid setup()
and just typeprintf
invoid loop()
?ironzionlion– ironzionlion2014年12月15日 14:42:26 +00:00Commented Dec 15, 2014 at 14:42 -
@ironzionlion if you read the printf documentation.. you will find it...letsjak– letsjak2014年12月15日 15:26:21 +00:00Commented Dec 15, 2014 at 15:26
-
@ironzionlion it's an LCD... tadaaaaaaa playground.arduino.cc/Main/Printfletsjak– letsjak2014年12月15日 15:26:56 +00:00Commented Dec 15, 2014 at 15:26
I love old questions, and I like C (and I like printf
for debugging).
There are two solutions (there are for sure more, I just mention this two):
Keep in mind the Arduino C (libraries) often does not support all escapes/formats (%f is missing)
First the "C-Style":
We just need a function, that writes a CHAR
to Serial and associate this function with stdout
(0)
#include <stdio.h>
char sputc(char c, FILE *) {
Serial.write(c);
return c;
}
void setup() {
fdevopen(&sputc, 0)1; // tell stdout (0) what to use
Serial.begin(115200);
printf("Hello World! We are here %2.2d\n",42);
}
The other solution - my preferred one because it takes about 700 byte less, is the following:
I use vsnprintf1 and varargs1 to fill a buffer - that easy.
void say(char const* fmt, ...) {
va_list vars;
char buf[80]; // that is a drawback.
va_start(vars, fmt);
vsnprintf(buf, sizeof(buf), fmt, vars);
Serial.print(buf);
}
void setup() {
Serial.begin(115200);
say("we are here %2.2d\n",10);
}
1this functions you find in internet.
main
in Arduino. You have thesetup
for initialization and theloop
for the continuing loopmain
in Arduino, but it is hidden seeedstudio.com/wiki/Where_is_Main_Function