Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    "resource not available" when assigning P9 / G16 as ADC

    Scheduled Pinned Locked Moved LoPy
    adc
    13 Posts 4 Posters 2.5k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • sorenS Offline
      soren @jmarcelino
      last edited by

      @jmarcelino thank you very much for chiming in. i'm baffled why this isn't stated in capital letters on the official pinout diagrams

      1 Reply Last reply Reply Quote 0
      • sorenS Offline
        soren @jmarcelino
        last edited by soren

        @jmarcelino so now I'm also getting this error from ADC1 pins... P14, to be precise

        robert-hhR 1 Reply Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh @soren
          last edited by

          @soren Are sou sure? I can do it here.

          import machine
          adc = machine.ADC()
          sensorPin = adc.channel(pin="P14")
          
          sorenS 1 Reply Last reply Reply Quote 1
          • sorenS Offline
            soren @robert-hh
            last edited by

            @robert-hh

            Hi robert, as far as I can tell - I'm doing the same thing as you.. here is my code, it's on line 9 ("sensorPin = adc...) it reports this error:

            import machine 
            from machine import Pin
            import time
            import pycom
            from machine import ADC
            from machine import PWM
            
            adc = machine.ADC()
            sensorPin = adc.channel(pin='P23', attn=ADC.ATTN_11DB) # G4 on expansion board
            ledArray = [0x7f0000, 0x007f00, 0x0000ff] # red, green, blue
            step_p = Pin('P8', mode=Pin.OUT)
            stepDir_p = Pin('P21', mode=Pin.OUT)
            
            
            class ColorSensor:
            
                dosingPump_p = Pin('P21', mode=Pin.OUT)
                dosingPump_p.value(0)
                # SERVO:
                pwm = PWM(0, frequency=50)
                servo_p = pwm.channel(0, pin='P22', duty_cycle=0.127)
                servo_p.duty_cycle(0.107)
                # 0.107 # upright position
                # 0.03 # pouring position
            
                balanceSet = False
                colourArray = [0, 0, 0]
                whiteArray = [0, 0, 0]
                blackArray = [0, 0, 0]
                avgRead = 0
            
                def checkBalance(self):
                    print("checking balance")
                    if not self.balanceSet:
                        self.setBalance()
            
                def setBalance(self):
                    print("setting balance")
                    time.sleep(5)
                    for num in range(0, 3): # set white bal
                        print(num)
                        pycom.rgbled(ledArray[num]) # set to color
                        time.sleep(0.1)
                        self.getReadings(5)
                        self.whiteArray[num] = self.avgRead
                        print('avgRead from bal ', self.avgRead)
                        pycom.rgbled(0x000000) # turn off led
                        time.sleep(0.1)
                    time.sleep(5)
                    for num in range(0, 3): # set black bal
                        pycom.rgbled(ledArray[num])
                        time.sleep(0.1)
                        self.getReadings(5)
                        self.blackArray[num] = self.avgRead
                        pycom.rgbled(0x000000) # turn off led
                        time.sleep(0.1)
                    self.balanceSet = True
                    time.sleep(5)
            
            
                def checkColour(self):
                    print("rinsing...")
                    for num in range(0, 3): # rinse glass 3 times
                        dosingPump_p.value(1) # take in water from aquarium
                        time.sleep_ms(500) # how much water to take in?
                        dosingPump_p.value(0) # turn off dosing pump
                        time.sleep(1)
                        servo_p.duty_cycle(0.03) # pour out water
                        time.sleep(4)
                        servo_p.duty_cycle(0.107) # turn back glass
                        time.sleep(4)
                    print('rinsing finished! Taking in water...')
                    dosingPump_p.value(1) # take in water from aquarium
                    time.sleep_ms(500) # how much water to take in?
                    dosingPump_p.value(0) # turn off dosing pump
                    time.sleep(1)
            
                    print('taking plunger down...')
                    for num in range(0, 201): # take plunger down
                        step_p.value(1)
                        time.sleep_ms(500)
                        step_p.value(0)
                        time.sleep_ms(500)
                        # check if plunger is all the way down and break ?
                    print('plunger is down! Waiting for analysis...')
            
                    time.sleep(1) # set to 3 minutes
                    print('analysis ready! Reading colours...')
            
                    for num in range(0, 3):
                        pycom.rgbled(ledArray[num]) # set to color
                        time.sleep(0.1)
                        self.getReadings(5)
                        self.colourArray[num] = self.avgRead
                        print('whitearray ', self.whiteArray[num], ' blackArray ', self.blackArray[num])
                        greyDiff = self.whiteArray[num] - self.blackArray[num]
                        print('greydiff: ', greyDiff)
                        self.colourArray[num] = (self.colourArray[num] - self.blackArray[num])/(greyDiff)*255
                        pycom.rgbled(0x000000) # turn off led
                        time.sleep(0.1)
            
                def printColour(self):
                    print('printing colour')
                    return self.colourArray[0], self.colourArray[1], self.colourArray[2]
            
                def getReadings(self, times):
                    reading = 0
                    tally = 0
                    for num in range(0, times+1):
                        reading = sensorPin()
                        print('reading ', reading)
                        tally = reading + tally
                        time.sleep(0.01)
                    self.avgRead = (tally)/times
                    print('avgRead ', self.avgRead)
                    #print('avgRead ', avgRead)
            
            robert-hhR 1 Reply Last reply Reply Quote 0
            • robert-hhR Offline
              robert-hh @soren
              last edited by robert-hh

              @soren P23 belongs to ADC2 ..... You should write P14, or G4, like you state in the comment

              sorenS 1 Reply Last reply Reply Quote 0
              • sorenS Offline
                soren @robert-hh
                last edited by soren

                @robert-hh yes, sorry i know - I have been trying with the following pins with that code i send you now: "13, 14, 15 and 23". 13, 14 and 15 is adc1.

                I think maybe I understand the problem a tiny bit better now:
                it happens only when I'm doing a soft reset. When I do a hard reset, it works again. Is this because a soft reset doesn't free up the pins maybe?

                1 Reply Last reply Reply Quote 0
                • H Offline
                  HenroRitchie @jmarcelino
                  last edited by

                  @jmarcelino Is this still the case. I am trying to access ADC2 on a Lopy4 and get the same error OSError: resource not available. ADC1 seems to be working fine.

                  J 1 Reply Last reply Reply Quote 0
                  • J Offline
                    jmarcelino @HenroRitchie
                    last edited by

                    @HenroRitchie
                    I've not followed the Pycom developments too closely but at low level ADC2 is only available when WiFi is off "ADC2 is used by the Wi-Fi driver. Therefore the application can only use ADC2 when the Wi-Fi driver has not started."

                    My guess is this complicates things in a dynamic Python environment where you can turn WiFi on and off at will so probably not a priority.

                    To be honest ADC1 is quite noisy so I avoid the ESP32 ADCs altogether and now use a more "industrial grade" external solution.

                    H robert-hhR 2 Replies Last reply Reply Quote 1
                    • H Offline
                      HenroRitchie @jmarcelino
                      last edited by

                      @jmarcelino Thank you, noted. Will design accordingly.

                      1 Reply Last reply Reply Quote 0
                      • robert-hhR Offline
                        robert-hh @jmarcelino
                        last edited by

                        @jmarcelino Hello Jose. Are you back again?

                        J 1 Reply Last reply Reply Quote 0
                        • J Offline
                          jmarcelino @robert-hh
                          last edited by

                          @robert-hh
                          Hey Robert! Good to see you're still around. Not back at Pycom but some collaboration brewing up, watch this space ;-)

                          1 Reply Last reply Reply Quote 0

                          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                          With your input, this post could be even better 💗

                          Register Login
                          • 1 / 1
                          • First post
                            Last post

                          Pycom on Twitter

                          Pycom