Important: Please read the Qt Code of Conduct -

Read DS18B20 with Raspberry pi

  • Hi,
    i have installed Qt Creator 3.2.1
    Based on Qt 5.3.2 (GCC 4.9.2, 32 bit)
    Built on May 11 2016 at 17:13:20 in my raspberry pi3

    i found header file and c++ code to read the temperature sensor ds18b20.
    i have a project file in qt creator where i designed two text labels for (+) and (-)
    i use them to increase or decrease the set point for temperature.
    i have a third text label3 (Temp) which should display the actual temperature.
    i added a LCDnumber module but i don't want to use it to display temperature but i want to keep it for later.

    So in my project there are some files like:
    -headers ( here i put the header file for ds18b20.h and there is already mainwindow.h)
    -source (here i have main.cpp and mainwindow.cpp)
    -form (here there is mainwindow.ui)

    i tried to put the cpp code to read the sensor in source and i don't know to use the textlabel3
    to read the sensor

    Here i can't attach my project file.
    so here is:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "ds18b20.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        this->connect(this->ui->button_plus, SIGNAL(clicked()), this,SLOT(on_button_plus_click()));
        this->connect(this->ui->button_minus, SIGNAL(clicked()), this,SLOT(on_button_minus_click()));
        this->counter = 19;
        delete ui;
    void MainWindow::on_button_plus_click()
    void MainWindow::on_button_minus_click()
    //void MainWindow::on_lcd_actual_temp_overflow()


    #include "mainwindow.h"
    #include <QApplication>
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        MainWindow w;;
        return a.exec();


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = 0);
    private slots:
        void on_button_plus_click();
        void on_button_minus_click();
        void on_lcd_actual_temp_overflow();
        void on_label_temp_linkActivated();
        Ui::MainWindow *ui;
        int counter;
    #endif // MAINWINDOW_H


    #ifndef DS18B20_H_
    #define DS18B20_H_
    #include <inttypes.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #define CELCIUS 0
    #define FAHRENHEIT 1
    #define BUS "/sys/bus/w1/devices/"
    #define TEMPFILE "/w1_slave"
    class DS18B20 {
    	DS18B20(const char* address);
    	virtual ~DS18B20();
    	uint8_t getUnits();
    	void setUnits(uint8_t);
    	float getTemp();
    	float CtoF(float);
    	uint8_t unit_;
    	char* address_;
    	char path[47]; // path should be 46 chars

    Now i don't know where to put this file:

    #include "DS18B20.h"
    DS18B20::DS18B20(const char* address) {
    	address_ = strdup(address);
    	unit_ = CELCIUS;
    	snprintf(path, 46, "%s%s%s", BUS, address_, TEMPFILE);
    DS18B20::~DS18B20() {
    float DS18B20::getTemp() {
    	FILE *devFile = fopen(path, "r");
    	if (devFile == NULL) {
    		printf("Count not open %s\n", path);
    	float temp = -1;
    	if (devFile != NULL) {
    		if (!ferror(devFile)) {
    			unsigned int tempInt;
    			char crcConf[5];
    			fscanf(devFile, "%*x %*x %*x %*x %*x %*x %*x %*x %*x : crc=%*x %s", crcConf);
    			if (strncmp(crcConf, "YES", 3) == 0) {
    				fscanf(devFile, "%*x %*x %*x %*x %*x %*x %*x %*x %*x t=%5d", &tempInt);
    				temp = (float) tempInt / 1000.0;
    	if (unit_ == CELCIUS) {
    		return temp;
    	} else
    		return CtoF(temp);
    uint8_t DS18B20::getUnits() {
    	return unit_;
    void DS18B20::setUnits(uint8_t u) {
    	unit_ = u;
    float DS18B20::CtoF(float temp) {
    	return temp * 1.8 + 32;

    I can find how to use temp object from cpp code
    but where to put this piece of code in my qt project ?
    Thank you
    for your help

  • Moderators

    @p_seg_rpi You can just put it in your MainWindow class in mainwindow.h as a private variable.

    Something like:

    class MainWindow : public QMainWindow
       DS18B20 *tempReader_;
    // then you can allocate it in the mainwindow constructor
       tempReader_ = new DS18B20(address);

    Then to get a temp update just call tempReader_->getTemp(). You could use a QTimer to do this every second (or whatever) and then update your GUI control with the proper temp after every call to getTemp().

    Hope that helps.

  • Hello, i am very new this qt. I tested this example but can not work. Please somebody all of them write source code. How to use configuration ds 18b20

Log in to reply