2

I've connected a temperature sensor (DS18B20) to my Pi and want to read it using 1-wire, but it doesn't work.

I have loaded the w1-gpio and w1-therm modules, added them to /etc/modules and rebooted a bunch of times.

This is the contents of /sys/bus/1w/devices:

pi@devpi ~ $ ls -l /sys/bus/w1/devices/
total 0
lrwxrwxrwx 1 root root 0 Apr  4 21:41 w1_bus_master1 -> ../../../devices/w1_bus_master1

As I understand my sensor should also appear in this directory as w1_xxxxsomething.

I assume my wiring is correct but you can never know. Here's a photo of it. I hope it's detailed enough.

Have I done something stupid and fried my pin?

Ghanima
  • 15,855
  • 15
  • 61
  • 119
fiskeben
  • 221
  • 4
  • 9

1 Answers1

2

It's unclear from your picture whether you are using the 5v0 or the 3v3 pin for the power rail of your breadboard. If you are using the 3v3 then it is highly unlikely that you have fried your pin. To move forward with our debugging, let's assume you haven't fried your pin.

It looks like you have wired the temperature sensor up correctly with the resistor across the +ve input and data pin. I am unfamiliar with the breakout board that you are using but I have a hunch you hooked the data pin to the wrong port - there is only one pin on the Pi which can read a DS18B20 and that is the GCLK pin (GPIO 4 / physical pin 7).

Once you have it wired up correctly you can debug from a terminal window. First run these from the Linux shell:

sudo modprobe w1-gpio
sudo modprobe w1-therm

Then change directory cd /sys/bus/w1/devices

In this folder if you run ls you should see another directory in the format 28-xxxxxxxxxxxx. This should correspond to the ID of the temperature sensor.

If you cd into this directory (protip: use cd 28*), you should see a file called w1_slave. This is the file which holds your temperature data. You can run cat w1_slave and it should return something along the lines of:

5b 01 4b 46 7f ff 05 10 b5 : crc=b5 YES
5b 01 4b 46 7f ff 05 10 b5 t=21687

In this case, t=21687 is your temperature in Celsius multiplied by 1000, i.e. 21.687 degrees C.

Sometimes, the top line will read NO instead of YES, in that case just wait a little while (10 seconds or so) and run cat w1_slave again.

I use a Python program to poll this file every 5 minutes which serves as a home temperature monitoring system. I believe the OS reads the device every 10 seconds or so.

nagyben
  • 773
  • 6
  • 10
  • The kernel module starts a 12-bit temperature conversation whenever you read the w1_slave file (which is specified to take no more than 750 ms). Not sure about the polling intervals at which it searches the bus for new devices, though. – n.st Apr 06 '14 at 10:44
  • You're right, I was using the wrong pin. I changed it to number 7 on the extension board and that works. – fiskeben Apr 06 '14 at 13:55
  • @fiskeben I was in the same exact situation and wiring the sensor to the P7 pin solved the issue and created the /sys/bus/w1/devices/28-*/ directory, but I cannot understand why the GPIO4 was mapped as P7 on our breakout board. – MaxChinni Nov 06 '15 at 15:57
  • @exantas thank you for the answer. I'm trying to guess what this breakout board is doing. Could you please clarify a point? I thought SCLK was GPIO11 (see [https://en.wikipedia.org/wiki/Raspberry_Pi]). We should connect our sensor to GPIO4 (GCLK), right? – MaxChinni Nov 06 '15 at 16:08
  • GPIO4 is physical pin 7. I have updated my answer to make it clearer – nagyben Nov 06 '15 at 17:49