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

How to Connect a Raspberry PI Touchscreen Display

The new official Raspberry PI touchscreen 7 inch display is now available and connects directly to a Raspberry PI board using a flat ribbon cable and power wires that are supplied with the display.

Raspberry PI Touchscreen Display Running Raspbian

Raspberry PI Touchscreen Display Running Raspbian

 

 

 

 

 

 

 

 

Touchscreen Power

The screen operates from a 5V supply and can be powered from the Raspberry PI header pins using the supplied wires. The screen can also be powered from its own external 5V power supply and has a micro USB connector identical to the one used to supply power to Raspberry PI boards. If a separate power supply is used to power the screen, it must be rated at 500mA or more.

Mounting a Raspberry PI Board on the Touchscreen

Terminal posts are provided at the back of the touchscreen that hold the touchscreen circuit board in place. A raspberry PI board can be mounted on the back of the touchscreen by attaching it with the four screws that are supplied with the screen to the terminal posts.

The image below shows a Raspberry PI 2 model B attached to the back of a Raspberry PI touchscreen.

Raspberry PI Board Mounted on Raspberry PI Touchscreen

Raspberry PI Board Mounted on the Back of the Touchscreen

Connecting a Raspberry PI to the Raspberry PI Touchscreen

It is important to make the connections between the Raspberry PI board and touchscreen correctly, especially the power to the screen when it is tapped from the header of the Raspberry PI board.

A flat ribbon cable is supplied with the screen for making a data connection between the raspberry PI board and screen. Jumper wires are supplied with the board for connecting 5V and GND from the Raspberry PI header pins to the touchscreen to supply power to the screen.

Full connection details including photos and a video can be found in the article on how to connect the official 7″ LCD Raspberry PI touchscreen to a Raspberry PI board.

Raspberry PI Touchscreen Connections

Raspberry PI Touchscreen Connections

 

Arduino Uno Interfacing GT-511C3 Fingerprint Scanner Demo Software

ADH Technology, the company that sells the GT-511C3 fingerprint scanner provide SDK software that includes a demo application for running on a PC. The demo software allows the scanner to be used to enrol fingerprints, each with a unique ID and then identify enrolled fingerprints when a finger is placed on the scanner later. The GT-511C3 fingerprint scanner demo software also includes several other functions that allow fingerprints to be deleted, display number of registered fingerprints, etc.

A USB to TTL level device is normally needed to connect the fingerprint scanner to a Windows PC to use the demo application.

Connecting the GT-511C3 Fingerprint Scanner to a PC using an Arduino

The article on interfacing the GT-511C3 to a PC running the demo software provides a quick hack that uses an Arduino Uno as a USB to TTL converter. The Arduino Uno uses a software serial port to connect to the fingerprint scanner because the only hardware serial port on the Uno is connected to the USB chip which is used as the interface to the PC USB port.

Serial data is relayed between the fingerprint scanner and demo software on the PC by a sketch running on the Arduino. The image below shows the demo application and interface to the scanner. Also see the full article that explains the hardware connections and software needed for the project.

GT-511C3 Fingerprint Scanner PC Demo Software using Arduino Uno

GT-511C3 Fingerprint Scanner PC Demo Software using Arduino Uno

How to Use Arduino Serial Ports

Arduino boards such as the Uno, MEGA2560 and Due all have a serial port that connects to the USB device port on the board. This port allows sketches to be loaded to the board using a USB cable. Code in a sketch can use the same USB / serial port to communicate with the PC by using the Arduino IDE Serial Monitor window, or a Processing application for example. The USB port appears as a virtual COM port on the PC.

This article shows how to use Arduino serial ports when additional serial ports are needed for a project.

Arduino Serial Ports Available

The serial port for programming the Arduino mentioned above is a hardware serial port. The microcontroller on the Arduino board has a hardware serial port built-in, so that after the port has been initialized by software, a byte sent to the port will be sent out serially by the hardware.

The Arduino Uno has only one hardware serial port because the microcontroller used on the Uno has only one built-in serial port. The Arduino MEGA 2560 and Arduino Due both have 3 extra hardware serial ports.

Serial Port Technical Details

The hardware serial ports referred to here are UART (Universal Asynchronous Receiver Transmitter) ports. They may be referred to as USART (Universal Synchronous Asynchronous Receiver Transmitter) ports in the microcontroller documentation if they are configurable in both synchronous and asynchronous modes.

Arduino Uno Serial Port

This image shows the only serial port available on the Arduino Uno highlighted in red. The port connects through a USB chip to the USB device port.

Arduino Uno Serial Port

Arduino Uno Serial Port

Arduino MEGA 2560 and Due

Both the MEGA 2560 and Due have 4 serial ports in total. One that connects through a USB port chip to the USB device port on the board and three extra serial ports that connect to pins on one of the pin headers of the board.

Arduino Due Serial Ports

Arduino Due Serial Ports

 

Arduino MEGA 2560 Serial Ports

Arduino MEGA 2560 Serial Ports

Pins 0 and 1 of the Due and MEGA connect serial port 0 through to the USB device port so that these Arduino boards are compatible with the pin numbering of the Uno and therefore with Arduino shields.

The extra serial ports are ports 1 to 3 with each port having a transmit and receive pin.

It is important to be aware that the MEGA 2560 serial port pins use 5V voltage levels, but the Due uses 3.3V voltage levels.

How to Use Additional Arduino Serial Ports

An extra serial port can be used on an Arduino Uno, but must be simulated in software by using the SoftwareSerial library.

Arduino Uno

The following code is taken from the article on serial communications with the GT-511C3 fingerprint scanner which connects the fingerprint scanner to a software serial port on an Arduino Uno.

#include <SoftwareSerial.h>

SoftwareSerial gtSerial(8, 7); // Arduino RX, Arduino TX

void setup() {
  Serial.begin(9600);    // serial / USB port
  gtSerial.begin(9600);  // software serial port
}

byte rx_byte = 0;        // stores received byte

void loop() {
  // check if byte available from USB port
  if (Serial.available()) {
    rx_byte = Serial.read();
    // send a byte to the software serial port
    gtSerial.write(rx_byte);
  }

  // check if byte available on the software serial port
  if (gtSerial.available()) {
    // get the byte from the software serial port
    rx_byte = gtSerial.read();
    Serial.write(rx_byte);
  }
}

To use the software serial port, first the header file for the software serial library must be included.

#include <SoftwareSerial.h>

Next create the software serial port, selecting the Arduino pins to use for receive (RX) and transmit (TX). Here pin 8 has been set as the receive pin and pin 7 as the transmit pin.

SoftwareSerial gtSerial(8, 7);

The software serial port had been given the name gtSerial which will be used in the sketch to refer to this serial port.

The port can now be checked for incoming data.

if (gtSerial.available()) {

If data is available, it can be read from the port.

rx_byte = gtSerial.read();

Data bytes can also be sent on the port.

gtSerial.write(rx_byte);

How to Use Additional Serial Ports on the Arduino MEGA 2560 and Due

The additional hardware ports on the Arduino MEGA 2560 and Due can be used in the same way as the main USB serial port is used in sketches, only changing the name of the port. The USB serial port, or serial port 0 is referred to as Serial in sketches. To use serial port 1, the name changes to Serial1. Serial ports 2 and 3 are referred to as Serial2 and Serial3.

This sketch shows serial port 3 being used which transmits on pin 14 of the MEGA or Due and receives on pin 15.

void setup() {
  // initialize serial ports
  Serial.begin(9600);    // USB serial port 0
  Serial3.begin(9600);   // serial port 3
}

byte rx_byte = 0;        // stores received byte

void loop() {
  // check for data byte on USB serial port
  if (Serial.available()) {
    // get byte from USB serial port
    rx_byte = Serial.read();
    // send byte to serial port 3
    Serial3.write(rx_byte);
  }
  // check for data byte on serial port 3
  if (Serial3.available()) {
    // get a byte from serial port 3
    rx_byte = Serial3.read();
    // send the byte to the USB serial port
    Serial.write(rx_byte);
  }
}

The additional serial ports are immediately available in the sketch without having to include any libraries.

Serial port 3 must first be initialized to the desired baud rate.

Serial3.begin(9600);

The port can be checked for incoming data.

if (Serial3.available()) {

If a byte has arrived on the serial port, it can be read.

rx_byte = Serial3.read();

A byte can be written to the serial port.

Serial.write(rx_byte);

Arduino Serial Port Resources

Arduino Website References for Software and Hardware

Projects, Articles and Tutorials

Can’t Find Arduino Due in Board List of IDE

Can’t find Arduino Due on the menu and therefore can’t load a sketch to the board? The Arduino Due does not appear in the board list of the Arduino IDE under the tools menu. Boards are normally listed under Tools → Board in the Arduino IDE, but if you can’t find the Arduino Due on the Board menu, additional software must be installed from within the IDE.

The Arduino Due used to have a separate IDE, but was later merged with the main Arduino IDE software. It is now a requirement to install the extra software in order to be able to program the Due.

Can’t Find Arduino Due on the Menu in the Arduino Software

In the image below, the Arduino Due is missing from the menu. The steps below show how to solve the problem and get the Arduino Due board listed on the menu.

Can't find Arduino Due on the menu - Arduino Due missing

Arduino Due Does not Appear in Board List of Menu

Installing Software for the Arduino Due

The following screen captures show how to install the software for the Arduino Due.

1. Boards Manager

Select the Boards Manager menu item from the Arduino IDE menu.
Tools → Board → Boards Manager …

Arduino IDE Boards Manager

Arduino IDE Boards Manager

2. Select Arduino SAM Boards

Click the Arduino SAM Boards item in the Boards Manager dialog box and then click the Install button.

Select Arduino SAM Boards

Select Arduino SAM Boards

It will take some time to install the software, depending on how fast the Internet connection is. After installation, click the Close button in the dialog box.

Programming the Arduino Due

After installation, the Arduino Due board will appear on the Board menu. Select Arduino Due (Programming Port) to allow Arduino sketches to be loaded to the board from the Arduino IDE. The USB cable must be plugged into the USB port labelled PROGRAMMING on the bottom of the board.

Arduino Due Programming Port

Arduino Due Programming Port