Floating Point Numbers don’t Print with sprintf in Atmel Studio AVR C

When writing C program code for 8-bit Atmel AVR microcontrollers and using Atmel Studio, functions such as printf() and sprintf() do not print floating point numbers of type float. Instead of printing a float to a string or standard output, a question mark is printed.

Question Mark is Printed from the sprintf Function Instead of Float Number

The image below shows the output from the serial port of an AVR microcontroller that printed a floating point number to a string using the sprintf() function. The string was then sent to a terminal program running on a PC. The C program was built in Atmel Studio 7 using the default project settings. As can be seen, floating point numbers don’t print with sprintf, but a question mark is printed instead of the expected number.

Floating Point Numbers don't Print with sprintf in Atmel Studio AVR C

Floating Point Numbers don’t Print with sprintf in Atmel Studio AVR C

Cause of the float Printing Problem

The problem occurs because Atmel Studio 7 uses the minimal version of the function that all the printf family of functions rely on. Using the minimal function reduces code size of the printf family of functions which is desirable when using microcontrollers, especially the smaller 8-bit AVR microcontrollers that have small amounts of memory. Floating point numbers are not supported by the minimal function, causing the question mark to be printed instead of the floating point number.

Printing float Numbers with sprintf using AVRs and Atmel Studio

To fix the floating point printing problem, the full function that the printf family uses must be linked into the program instead of the minimal function. The article on how to print floating point numbers in AVR C code with Atmel Studio 7 shows how to include the full function by changing linker settings in Atmel Studio.

After changing setting in Atmel Studio, the sprintf function works properly and prints the floating point number to the terminal as shown in the image below.

Printing float Numbers with an AVR Microcontroller using sprintf

Printing float Numbers with an AVR Microcontroller using sprintf

ATtiny2313 8-bit Microcontroller Course

This ATtiny2313 course is presented as a multi-part tutorial and covers software development and hardware development using the ATtiny2313 AVR microcontroller from Atmel.

Connecting the AVR Dragon Programmer / Debugger to a Breadboard Circuit

An AVR microcontroller board will usually have a header for connecting a programmer / debugger such as the Atmel AVR Dragon. This is not the case when building a microcontroller circuit on breadboard.

This tutorial shows how to connect the AVR Dragon to an AVR microcontroller breadboard circuit for programming and debugging the microcontroller.

Getting Started with AVR Microcontrollers

This tutorial on Starting AVR 8-bit Development shows what hardware and software is needed to start programming 8-bit AVR microcontrollers in C. The tutorial shows step-by-step how to set up hardware and install software to get your first AVR C program compiled and loaded to a breadboard circuit.

The free Atmel Studio software is used to create the program and the AVRISP mkII programmer is used to load the program to the AVR microcontroller. The tutorial uses the ATtiny2313 microcontroller.