Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:20 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Pico Basic Pin in use error reading temperature sensor

Author Message
k2backhoe
Regular Member

Joined: 04/12/2021
Location: United States
Posts: 47
Posted: 04:50pm 16 Feb 2022
Copy link to clipboard 
Print this post

Has anyone else run into this?  SOmetimes when I try to do a

tc = TEMPR(gp22)       instruction, I get this persistent error
Error : Pin 29 is in use

and once the error shows up, I can't find any way to clear it.  powering down the pico overnight clears the error, but when it shows up (randomly as far as I can tell), I can no longer read this sensor.  I have two identical sensors on pins 21 and 22.  If one gets this error, the other one also does.

I tried setpin gp22,off       but it gives the same error.  OpSys version is
?mm.ver
5.070401

Any hints are appreciated?

thanks, k2
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2640
Posted: 09:39pm 16 Feb 2022
Copy link to clipboard 
Print this post

Does this, from the manual, make any difference?

"TEMPR START pin [, precision]

This command can be used to start a conversion running on a DS18B20
temperature sensor connected to 'pin'.
Normally the TEMPR() function alone is sufficient to make a temperature
measurement so usage of this command is optional.
This command will start the measurement on the temperature sensor. The
program can then attend to other duties while the measurement is running
and later use the TEMPR() function to get the reading. If the TEMPR()
function is used before the conversion time has completed the function will
wait for the remaining conversion time before returning the value.
Any number of these conversions (on different pins) can be started and be
running simultaneously."

Without TEMPR START perhaps the Pico is too quick, expecting data before the DS18B20 is ready.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 10:50pm 16 Feb 2022
Copy link to clipboard 
Print this post

Are you running the DS18B20 powered or parasitic? If the latter try normal powered. Are you sure the chips are genuine? Most likely issue is that the response is only partly being received with enough to defeat the "no-response" timeout but not a complete data sequence
 
k2backhoe
Regular Member

Joined: 04/12/2021
Location: United States
Posts: 47
Posted: 08:23pm 18 Feb 2022
Copy link to clipboard 
Print this post

Matherp: I am running powered by the 3.3V.  The devices are "Gikfun DS18B20 Temperature Sensor Waterproof Digital Thermal Probe Sensor" from Amazon, sealed in a Stainless tube.  Not sure how to check for authenticity.  I may have noise on the 3.3V line I am working on eliminating it to see what difference it makes.

Also Matherp:  is there any accessible documentation on fault error messages not covered in the manual?

Phil99: I seem to get a few errors with or without using the tempr start command.  I will also investigate this further
 
expo
Newbie

Joined: 10/01/2016
Location: Australia
Posts: 25
Posted: 06:13am 19 Feb 2022
Copy link to clipboard 
Print this post

When I had this problem I had declared A PWM which was using the same pin (which doesn't name the pin, causing the confusion to me).

expo Greg.
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 671
Posted: 09:00pm 23 Feb 2022
Copy link to clipboard 
Print this post

I tested my DS18B20 sensors and I have the same problem. No matter what configuration or what sensor (got many of them). Also I have a lot of false readings like -1002 and 503, for example (instead of 22'C). It is no noise what so ever. I am pretty sure it _must_ have something to do with counterfeit DS18B20 chips. A year ago I started a topic in this forum with the same issue (but for the CMM2). The genuine DS18B20 chip worked with the CMM2 but sadly I can't find my genuine one because I only had one, all my other are "fake", like 99% on the market.

But the question for Peter: I can fully understand, that you won't support counterfeit chips and you stick to the original datasheet, but I know for sure (because I tested over 20 chips counterfeit against genuine on the Arduino with the DS18B20 library) all my fake chips work without any problem on the Arduino. Just wondering what the Arduino librarys make them work and the Pico and CMM2 does not.

Whatever... I don't expect you to inplement a solution for this fake chips, but I am sure the problem of "k2backhoe" is that the chip is counterfeit. The genuine costs 3 times as much here in Germany and is NOT available via eBay.

Greetings
Daniel
Edited 2022-02-24 07:04 by Amnesie
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 10:21pm 23 Feb 2022
Copy link to clipboard 
Print this post

Try a level shifter and run one on 5V. DO NOT USE 5V ON A PICO INPUT. They *should* work over 3V0 to 5V5, but you never know.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 10:47pm 23 Feb 2022
Copy link to clipboard 
Print this post

If someone wants to send me one of these non-working sensors I can look at it on the scope (send me a PM). All of mine work and since I bought a batch of 10 for < GBP5 it is unlikely they are genuine
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 04:31am 24 Feb 2022
Copy link to clipboard 
Print this post

I resurrected the code I used to investigate the 'odd' DS18B20s that are around.
You can read the original discussion here https://www.thebackshed.com/forum/ViewTopic.php?TID=12339&PID=152019#152019#152037

There are a lot of pages.

I modified the code to run on the picomite.
MMBasic has ONEWIRE commands available so you can talk to the DS18B20 and similar without having to use the TEMPR command.
You need to set PinNbr to suit your system.

This code talks directly to the suspect device and the returned data may help work out what's wrong.
The rom code may indicate a suspect and the scratchpad tells us if the device was able to change resolution.
'DS18x20 test code by TassyJim
DIM INTEGER PinNbr = 24 ' 22
PRINT
PRINT
PRINT "Family name: ";FamilyName$(PinNbr)
PRINT "Rom Code:    ";getRomCode$(PinNbr)
PRINT "ScratchPad:  ";getScratchpad$(PinNbr)

IF powerMode(PinNbr) THEN
  PRINT "Power mode   External"
ELSE
  PRINT "Power mode   Parasitic"
ENDIF
 
PRINT

print "temp = "; tempr(PinNbr)
FOR n = 9 TO 12
PRINT
  x = setResolution(PinNbr, n)
  PRINT "ScratchPad:  ";getScratchpad$(PinNbr)
  'pause 10000
  TIMER = 0
  PRINT "Temperature: ";getTemp(PinNbr);" ";TIMER;"mS,";n;" bit"
NEXT n

END


FUNCTION getRomCode$(PinNbr AS INTEGER)
' get ROM Code - useful if you want more than 1 device on wire
LOCAL INTEGER a1,a2,a3,a4,a5,a6,a7,a8
ONEWIRE WRITE PinNbr,1,1,&h33 'read ROM code
ONEWIRE READ PinNbr,0,8,a1,a2,a3,a4,a5,a6,a7,a8
getRomCode$ = HEX$(a1,2)+" "+HEX$(a2,2)+" "+HEX$(a3,2)+" "+HEX$(a4,2)+" "+HEX$(a5,2)+" "+HEX$(a6,2)+" "+HEX$(a7,2)+" "+HEX$(a8,2)
END FUNCTION

FUNCTION getScratchpad$(PinNbr AS INTEGER)
'ONEWIRE Write PinNbr,1,9,&h55,a1,a2,a3,a4,a5,a6,a7,a8  'read from scratchpad
ONEWIRE WRITE PinNbr,1,2,&hcc,&hbe
ONEWIRE READ PinNbr,0,8,a,b,c,d,e,f,g,h
getScratchpad$ = HEX$(a,2)+" "+HEX$(b,2)+" "+HEX$(c,2)+" "+HEX$(d,2)+" "+HEX$(e,2)+" "+HEX$(f,2)+" "+HEX$(g,2)+" "+HEX$(h,2)
END FUNCTION

FUNCTION powerMode(PinNbr AS INTEGER) AS INTEGER
' check whether power external (1) or parasitic (0)
ONEWIRE RESET PinNbr
ONEWIRE WRITE PinNbr, 1,2,&hcc, &hb4
ONEWIRE READ PinNbr,4,1,powerMode
END FUNCTION

FUNCTION getFamily(PinNbr AS INTEGER) AS INTEGER
getFamily = VAL("&h"+LEFT$(getRomCode$(PinNbr),2))
END FUNCTION

FUNCTION FamilyName$(PinNbr AS INTEGER)
LOCAL INTEGER fn
fn = getFamily(PinNbr)
SELECT CASE fn
  CASE 16
    FamilyName$ = "DS1820/DS18S20"
  CASE 34
    FamilyName$ = "DS18S22"
  CASE 40
    FamilyName$ = "DS18B20"
  CASE ELSE
    FamilyName$ = "Unknown"
END SELECT
END FUNCTION

FUNCTION setResolution(PinNbr AS INTEGER,r AS INTEGER)
r = r-9
IF r < 0 THEN r = 0
IF r > 3 THEN r = 3
r = r*32+31
ONEWIRE WRITE PinNbr, 1,5,&hcc, &h4E, &hFF,&hFF,r
END FUNCTION

FUNCTION getTemp(PinNbr AS INTEGER) AS FLOAT
LOCAL INTEGER fn, t, T1, T2, a,b,c,d,e,f,g,h
LOCAL FLOAT Value
fn = getFamily(PinNbr)
power = powerMode(PinNbr)
ONEWIRE WRITE PinNbr,1,2,&hcc,&hbe
ONEWIRE READ PinNbr,0,8,a,b,c,d,e,f,g,h
ONEWIRE RESET PinNbr                        ' reset before command
ONEWIRE WRITE PinNbr, 0, 2, &hcc, &h44      ' start conversion

'read external when b goes high, for parasitic just wait
'IF power = 0 THEN 'parasitic
  'PAUSE 750
'ELSE      ' external power
   't = TIMER
  'DO
    'IF TIMER - t > 1000 THEN
      'Value = 1000
      'EXIT DO
    'ENDIF
    'ONEWIRE READ PinNbr, 6 , 1 , b            ' conversion done?
  'LOOP UNTIL b = 1
'ENDIF
pause (50 << (e+1)/32) ' pause time varies depending on resolution

IF Value = 0 THEN ' we have not timed out yet
  ONEWIRE WRITE PinNbr, 1, 2, &hcc, &hbe      ' command read data
  ONEWIRE READ PinNbr, 2, 2, T1, T2           ' get the data
  ONEWIRE RESET PinNbr
  'print T1,T2, T2 AND &b1000,(T2 AND &b111)
  SELECT CASE fn
    CASE 16 'DS18S20 or DS1820
      ONEWIRE WRITE PinNbr,1,2,&hcc,&hbe
      ONEWIRE READ PinNbr,0,8,a,b,c,d,e,f,g,h
      IF T2 AND &h80 THEN       'if MSB of T2=1 then negative
        'Read 12bit resolution and adjust
        'truncate 0.5deg value (or use integer division \)
        T1 = T1 AND &hFE
        T1=T1 / 2                   'make whole degrees
        'add compensation values read from scratchpad
        T1=T1*16                  'make lsb 1/16 degree
        T1 = T1 -4 +(16 - g)    'add 12 bit value
        'take 2s complement
        T1 = (T1 XOR &hFF) + 1
        Value = -T1/16                'make decimal value in degrees
      ELSE                              'positive temp
        Value = T1 / 2                '9bit value
        'Read 12bit resolution and adjust
        T1 = T1 AND &hFE      'truncate 0.5deg(or use integer division \)
        Value = T1/2- 0.25 + (16 - g) /16   '12 bit value
      ENDIF
    CASE 34, 40 ' DS18S22 or DS18B20
        IF (T2 AND &b1000) THEN       'negative temp
        'make 2s complement (1s complement+1)
        T2 = (T2 XOR &hFF)
        T1 = (T1 XOR &hFF)+1
        IF T1=1 THEN T2=T2+1        'add the carry if required
        Value = -((T2 AND &b111) * 256 + T1) / 16
      ELSE                          'positive temp
        Value = ((T2 AND &b111) * 256 + T1) / 16
      ENDIF
    CASE ELSE
      Value = 1000
  END SELECT
ENDIF
getTemp= Value
END FUNCTION

I found that my DS18B20s work better without any external pullup.

I always use external power rather than parasitic but it shouldn't make any difference to MMBasic.

Note to Peter:
My code usually polls the data line and waits for the end-of-conversion signal.
ON the MX170, that works reliably but on the picomite the data line goes high immediately so I had to use the calculated delay instead.
Not a problem for temperature measurement but it might get interesting with other one wire devices. I will do some more investigating when time permits but it looks like the strong pullup is on when it shouldn't be.
Edited 2022-02-24 14:34 by TassyJim
VK7JH
MMedit
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:01am 24 Feb 2022
Copy link to clipboard 
Print this post

I finally found my suspect chip.
The TEMPR command works correctly.

My do-it-yourself code doesn't, but it does for the known genuine ones.
There seems to be a bit shift going on. Likely a timing issue.

Jim
VK7JH
MMedit
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025