danjperron / BitBangingDS18B20

Method to access the DS18B20 sensor using Rapsberry Pi GPIO
41 stars 29 forks source link

Your program is not working today on RPI4 #18

Open Bagunda opened 1 week ago

Bagunda commented 1 week ago

It was not easy to understand what to do to start the program and how the programs are arranged. Compiled DS18B20Pi5Scan DS18B20Scan

Disabled 1-wire support via raspi-config. But left the DS18B20 on the same GPIO 4. Rebooted. Launched the program ./DS18B20Pi5Scan -gpio 4. Silence...

Launched the program ./DS18B20Scan -gpio 4. Silence...

danjperron commented 1 week ago

Hi Bagunda,

long time that I didn't check the bitbanging with the newest O.S.

step by step 1 - cd 2 - git clone https://github.com/danjperron/BitBangingDS18B20 3 - cd BitBangingDS18B20

If you are using the 32 bits O.S. use DS18B20Scan

4 - gcc -lrt -o DS18B20Scan DS18B20Scan.c

If no error you could check if the program is compiled by using the help

daniel@pi4test:~/BitBangingDS18B20 $ ./DS18B20Scan --help
usage :

 ./DS18B20Scan -gpio n [-xbits] [-s] [-t delay] [-f filename]

 -gpio n     ->  n specify the BCM GPIO number to check
 -xbits      ->  x set the number of bits -9bits,-10bits,-11bits and -12bits
 -t delay    ->  delay is the time in ms to wait after conversion
 -s          ->  Scan for sensor
 -f filename ->  filename to read sensor id and return information

To test it connect a DS18B20 using any GPIO. The best way to test is to not use GPIO4 because of 1 wire. Don't forget to add a pull up resistor around 4k7 to the data pin.

Now if you do ./DS18B20Scan -gpio x replace the x with the GPIO you specify.

This is my old Pi result using GPIO4 (1-wire disabled).

pi@raspberrypi:~/BitBangingDS18B20 $ ./DS18B20Scan -gpio 4
28-000004575F0A : 12 bits  Temperature:  19.31 +/- 0.06 Celsius
pi@raspberrypi:~/BitBangingDS18B20 $

On 64bits O.S. you need to use the DS18B20Pi5Scan (gpiod)

3 - sudo apt-get install gpiod libgpiod-dev libgpiod-doc (reboot after) 4 - gcc -o DS18B20Pi5Scan DS18B20Pi5Scan.c -l gpiod

If no error you could check if the program is compiled by using the help

daniel@pi4test:~/BitBangingDS18B20 $ ./DS18B20Pi5Scan --help
usage :

  ./DS18B20Pi5Scan -gpio n [-xbits] [-s] [-t delay] [-f filename]

 -gpio n     ->  n specify the BCM GPIO number to check
 -xbits      ->  x set the number of bits -9bits,-10bits,-11bits and -12bits
 -t delay    ->  delay is the time in ms to wait after conversion
 -s          ->  Scan for sensor
 -f filename ->  filename to read sensor id and return information

Don't forget to add a pull up resistor around 4k7 to the data pin. I'm on remote to my Pi4 I can't add a DS18B20 on GPIO to show you the result but I will check it tonight.

regards, Daniel Perron

Bagunda commented 1 week ago

When enabling 1-wire via raspi-config, 5 sensors are visible, but not stably. When adding more DS18B20 sensors, all sensors disappear and incomprehensible MAC addresses appear.

I turn off 1-wire via raspi-config, then reboot... And I try to use your program on the same GPIO4. I can't change GPIO, because the object is far away via VPN

64bits O.S.

cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@rpi432:~/BitBangingDS18B20#

image

danjperron commented 1 week ago

Ok looks like that the cable has a lot of capacitance.

Try to delay the response pulse use 10 or 15 microsecond instead of 2

DS18B20Pi5Scan.c line 195 (ReadBit) and line 221 ReadByte()

unsigned char ReadBit(void)
{
   unsigned char rvalue=0;
   // PIN LOW
   GPIO_CLR
   DELAY1US
   // set INPUT
   GPIO_SET
   DelayMicrosecondsNoSleep(10);
   if(GPIO_READ!=0)
    rvalue=1;
   DelayMicrosecondsNoSleep(60);
   return rvalue;
}

And change the ReadByte() also

unsigned char ReadByte(void)
{

   unsigned char Mask=1;
   int loop;
   unsigned  char data=0;
   int loop2;
   for(loop=0;loop<8;loop++)
     {
       //  set output
       GPIO_CLR
       //  PIN LOW
       DELAY1US
       //  set input
       GPIO_SET
       // Wait  2 us
       DelayMicrosecondsNoSleep(10);
       if(GPIO_READ!=0)
       data |= Mask;
       Mask*=2;
       DelayMicrosecondsNoSleep(60);
      }

    return data;
}