Measuring temperature using digital sensor DS18B20 and Arduino Multifunction Shield on FRDM K64F
This tutorial describes a simple application for measuring temperature using the digital sensor DS18B20 and the Arduino Multifunction Shield connected to FRDM K64F. You can use new feature Expansion Boards (expansion shields) included in the Pins tool v9. This feature enables quick integration of shields in an application without the need to study manuals.
In this paper, you can find the environment setup, 1-wire protocol basics, and the steps of preparing a code with MCUXpresso tools (Peripherals and Pins tool).
Setup environment
- Install MCUXpresso IDE v11.3.0 or later and SDK FRDM-K64F 2.8.2 or later.
- Use the FRDM K64F EVK board https://www.nxp.com/webapp/Download?colCode=FRDMK64FUG.
- Connect the Arduino Multifunction shield (Expansion Board) to the FRDM K64F EVK board and DS18B20 to the Arduino shield.
- Connect the EVK board to the PC via a USB cable.
Figure 1 FRDM K64F EVK board with Arduino Multi-Function shield
- Have the expansion board definition file arduino_multifunction_ds18b20.xml. For detailed instructions on how to create the expansion board see the following article: Creating expansion board definition file for Arduino Multifunction shield
DS18B20 Programmable Resolution 1-Wire Digital Thermometer
See the documentation https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
This tutorial uses powering with an external supply.
Figure 2 Powering the DS18B20 with an External Supply
See the timing diagrams for communication with the DS18B20, needed for writing 1-Wire communication protocol.
Figure 3 Initialization Timing
Figure 4 Read/Write Time Slot Timing Diagram
The application only uses the following 1-wire protocol commands:
- ‘CCh’ skip ROM command - only one slave on the bus supported
- ’44h’ convert temperature
- ‘BEh’ read DS18B20 registers (temperature)
Create the application in MCUXpresso IDE
Begin with the hello_word example from SDK, rename the project to frdmk64_termometer.
Steps to configure timer in the Peripherals tool:
- Go to the Peripherals tool and add/configure the PIT component. Resolve the new Error in Problems view by adding the SDK component to the project.
Figure 5 PIT component in the Peripherals tool
Steps to apply the expansion board and configure pin in Pins tool:
- Apply the expansion board (arduino_multifunction_ds18b20.xml) from menu Pins -> Apply Expansion Board menu
Figure 6 Apply expansion board dialog
- The Expansion Board Routing dialog is automatically opened after step 1. It provides the possibility to route signals to processor pins that are connected to the expansion board. By default, all pins are routed automatically, see below. Keep only pin A4 routed which is needed for our thermometer application. Unroute the unneeded pins by clicking the Route column or later in the Routing Details view, then click Apply.
Figure 7 Expansion board routing dialog
- The action results in the following setting. When selecting Expansion board in the Labels combo, you see the labels as they are marked on the Arduino shield.
Figure 8 Expansion header view
- In the Routing Details view, set the electrical properties of the pin (Direction, Pull select, Pull enable).
Figure 9 Electrical properties in the Routing Details view
- Click Update Code to update the project with the code generated by the tools.
Write the thermometer application and 1-wire protocol (thermometer.c)
Rename the source file hello_world.c to thermometer.c and follow the steps below. Find the completed thermometer.c in the project.
- Use the PIT timer to create methods delayMS() and delayUS() for delays of μs and ms. See the PIT interrupt handler PIT_CHANNEL_0_IRQHANDLER() function.
- Write methods GPIO_low() and GPIO_high() to set the ARDUINO_A4_PIN to logical 0 and 1. These methods are based on the code which is in the screenshot below.
Figure 10 Generated code for ARDUINO_A4_PIN
- Write methods for reading/writing bytes to the 1-wire bus.
Figure 11 Methods for reading/writing bytes
- Write methods to request the temperature conversion and read the temperature, see dsRequestConvertTemp() and dsReadTemp(). Finally, use the methods in main().
Build the project and start debugging.
Figure 12 Output from the application