How to read a 1-wire temp sensor on an Ubuntu 22.04 PC?

Bo Berglund bo.berglund at gmail.com
Sat Mar 16 12:31:37 UTC 2024


On Wed, 13 Mar 2024 09:24:31 +0000, Chris Green <cl at isbd.net> wrote:

>On Wed, Mar 13, 2024 at 10:08:51AM +0100, Bo Berglund wrote:
>> I would like to know if it is possible to connect a DS18B20 1-wire device to an
>> Ubuntu PC (running on PC hardware) and if so how?
>> 
>> On a Raspberry Pi running the Pi-OS (Debian variant) it is just a matter of
>> configuring the RPi for the 1-wire use and then connect the sensor to +Vcc, GND
>> and the 1-wire line with a pull-up to Vcc and it works.
>> 
>> Can something similar be done on PC hardware running Ubuntu?
>> 
>> I have a few candidates, both Mini type and laptops.
>> 
>Get a USB 1-wire interface, that will work on any old PC, I can dig
>out details if you can't find one.

I decided to stick with the RaspberryPi Zero-W device as interface to the temp
sensors instead.
For that I have a simple solution using just the RPiZero board, one 4K7 pull-up
resistor and the DS18B20 sensor device (plus 3 wires to hook it up).

On the Pi one can easily enable the 1-wire interface, which uses the GPIO-04
pin.
And then this code to extract the temperature:

-----------
#!/bin/bash
IP="192.168.xxx.yyy" #adddress of MQTT broker
givare8="28-3ce1e3814893" #TO-92 style sensor soldered into the board

Temp1=$(cat /sys/bus/w1/devices/${givare}/temperature)
printf -v Temp1 "%06d" "$Temp1"
if [ $? != "0" ]; then
  echo "Error, no response from sensor!"
  exit 1
fi
CurrTemp1=$(sed 's/.\{3\}$/.&/' <<< "$Temp1")

#Show result:
echo "Temp1 = $CurrTemp1"
eval "mosquitto_pub -h ${IP} -t \"sensors/cottage/temp\" -m \"$CurrTemp1\""

exit 0
------------


-- 
Bo Berglund
Developer in Sweden




More information about the ubuntu-users mailing list