1-Wire Driver with DS18B20 Temperature Sensor Demo

Table of Contents
Description

This QuickByte shares two SPIN2 code examples that enable the P2 to communicate with 1-wire devices. We are using the common DS18B20 1-wire temperature sensor in these examples, although the driver could be extended for any 1-wire device.

  • Example 1: Search 1-wire bus for DS18B20 device, display the serial number and temperature on a serial terminal.
  • Example 2: Based on a known DS18B20 serial number, read the temperature and display on a serial terminal.
Connect Up Your Hardware

 

1 = GND (Connect to one of the GND pins on your P2 breakout board)


2 = P2 I/O Pin (Connect to an I/O pin, P7 used in the example code)


3 = 3.3V (Connect to One of the Vxx pins on your P2 breakout board)

Load the Code

The two example code archives are available to download from the Source Code link below.

Open Propeller Tool and load the code example.

Tip: Unzip the example code archive and then use PropellerTool to open the main file, which in the case of these examples can be identified by the _demo tag at the end of the filename. You can always find the main file of any PropellerTool archive by consulting the “_README.txt” file inside the archive. All the source code files are listed, with the main file at the top.

In the example code files, ensure the 1-wire I/O pin matches the pin connected to your sensor. We have used pin 7 in the example, but you can change it to any spare I/O pin you prefer:

 

And for example 2, also ensure the serial number of the 1-wire device matches your serial number! You can read your serial number using code example 1.

Within PropellerTool:

  1. Press F7 to identify your P2, and make a note of the COM port number
  2. Press F10 to load the Code into the P2 microcontroller
  3. Press F12 to open the Parallax Serial Terminal (PST) software

 

The code will now be running in your P2, and will be waiting to receive a keypress on the serial terminal before it starts transmitting data. This allows you time to set up your serial terminal as follows:

Within the PST:

  1. Select the correct COM port number for your microcontroller (from the F7 step above)
  2. Click Enable
  3. Click inside the text box near the top of the PST window, then press enter

 

Once you press Enter, the output from the P2 program will be displayed in the terminal. This is the typical output from Example 1:

Connecting 1-wire devices to the Propeller P2 is very simple, and in most cases they do not require any additional components, such as pull-up or series resistors, as the P2 SmartPins can emulate those functions.

For help please feel free to ask your questions here, or for a wider audience of helpers at our Forums: https://forums.parallax.com.

Programming Language
Document Author
Source Code Author
5 1 vote
Article Rating
Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Loading...