• Measuring current with the ADC

    Discussion adc sample rate app developmen
    7
    0 Votes
    7 Posts
    3k Views
    robert-hhR
    @robert-hh Another variant for measuring, which determines the difference between a minimal and maximal value over a window of samples, which should be at least a period of the signal to be tested, e.g. 20 ms for a 50Hz net. More simple than the last example, and immune to offset drifts. Still, the ADC is bad. The noise is a little bit lower if WiFi is switched off, but that only matters at small signals. # import math import array from machine import ADC, Timer, idle samples = 600 freq = 50 _BUFFERSIZE = const(64) # # acquire ADC values at a fixed rate/s, which is given in # the constructor. The second argument is the size # of a ringbuffer, the third a string with the Pin name # class Acquire: def __init__(self, samples, buffersize, pin): self.put = 0 self.get = 0 self.buffersize = buffersize self.buffer = array.array("h", 0 for _ in range(self.buffersize)) self.adc = ADC(bits=9) self.apin = self.adc.channel(pin=pin, attn=ADC.ATTN_11DB) self._alarm = Timer.Alarm(self.read_adc, 1/samples, periodic=True) def stop(self): self._alarm.cancel() def read_adc(self, alarm): self.buffer[self.put] = self.apin.value() self.put = (self.put + 1) % self.buffersize def next(self, period): vmin = 4096 vmax = 0 for _ in range(period): while self.get == self.put: idle() # calculate the moving average over the last three values: value = (self.buffer[self.get] + self.buffer[(self.get - 1) % self.buffersize] + self.buffer[(self.get - 2) % self.buffersize]) / 3 vmin = min(value, vmin) vmax = max(value, vmax) self.get = (self.get + 1) % self.buffersize return vmax - vmin # # calculate a moving average # The constructors argument is the window size # class Average: def __init__(self, size): self.size = size self.buffer = array.array("f", 0.0 for _ in range (size)) self.put = 0 self.sum = 0.0 def avg(self, value): self.sum -= self.buffer[self.put] self.buffer[self.put] = value self.put = (self.put + 1) % self.size self.sum += value return self.sum / self.size def run(noise=1.2): acq = Acquire(samples, _BUFFERSIZE, 'P20') avg_res = Average(freq) noise_sq = noise * noise try: i = 0 while True: res = avg_res.avg(acq.next(samples/freq)) i += 1 if i == freq: # remove the noise floor res = math.sqrt(abs(res*res - noise_sq)) print("{:6.2f} ".format(res/20.2)) i = 0 except KeyboardInterrupt: acq.stop() print("Handler stopped") run()
  • ADC change the attenuation level to 11dB

    WiPy 2&3 adc attenuation
    3
    0 Votes
    3 Posts
    1k Views
    overlanderO
    @robert-hh thank you for your quick reply.I see it-the quotes are wrong :(
  • "resource not available" when assigning P9 / G16 as ADC

    LoPy adc
    13
    0 Votes
    13 Posts
    3k Views
    J
    @robert-hh Hey Robert! Good to see you're still around. Not back at Pycom but some collaboration brewing up, watch this space ;-)
  • What is ADC 2 there for on the pin out?

    Discussion adc wipy 2.0 pinout
    2
    0 Votes
    2 Posts
    1k Views
    J
    @panos29 P20 is an ADC1 pin, are you sure you can't use it? As for the ADC2 that is not currently supported in the Python side because it it's a bit of an oddball: due to hardware/firmware limitations you can't use WiFi and ADC2 at the same time. For now to use it you'd have to program in C and have WiFi off.
  • ADC Accuracy

    LoPy adc lopy
    3
    0 Votes
    3 Posts
    2k Views
    D
    Ouch. That's probably something that should be documented by Pycom. Thanks for the link!
  • ADC Range seems to switch

    WiPy 2&3 adc
    6
    0 Votes
    6 Posts
    2k Views
    L
    @guyadams99 Simple mistakes are always harder to find..
  • Reading analog voltage with ADC and attenuation

    LoPy adc sensor attenuation
    9
    0 Votes
    9 Posts
    4k Views
    H
    the scale factor is Vcc / 5120 per 1 mm
  • Using P9 as ADC input when using PyTrack

    LoPy adc pytrack pins
    3
    0 Votes
    3 Posts
    1k Views
    L
    @tholden To see some info about problems with ADC which @jmarcelino pointed here is the link to issue report https://github.com/espressif/esp-idf/issues/164 and also interesting https://github.com/espressif/arduino-esp32/issues/92
  • Analog CO2 Sensor for LoPy, can 5v pin power it?

    LoPy co2 lopy adc 5vdc sensor
    15
    0 Votes
    15 Posts
    5k Views
    bmarkusB
    @ledbelly2142 said in Analog CO2 Sensor for LoPy, can 5v pin power it?: @bmarkus The COZIR sensors look nice. Are they in the sub $100 range? First hit of Google search gives $109 USD https://www.co2meter.com/products/cozir-0-2-co2-sensor As an indication you can visit the webshop of ChipCAD, a Hungarian distributor https://shop.chipcad.hu/Welcome/Default.aspx?scenarioID=360&pid=1878 Prices are in HUF (Hungarian Forint), VAT not included. At the moment 1 USD = 262.6 HUF But it may vary by distributor, quantity, shipping conditions, custom, etc. and model. You can ask GSS to advice where to buy it, they are good.
  • ADC1 and ADC2 pins

    LoPy lopy ldr adc
    2
    0 Votes
    2 Posts
    1k Views
    J
    @MrMarty77 Only ADC1 is supported at the moment, the core ESP32 SDK doesn't support it yet, it may take a while: https://github.com/espressif/esp-idf/pull/466 Fyi, we're a bit hesitant to make ADC2 available because it is also used for pretty important control loops inside the WiFi (and maybe BT, I don't remember) drivers. We want to make sure there's no chance of these and the user ADC calls to interfere with each other.
  • ADC Interrupts

    Moved Enhancements lopy adc interrupt
    2
    0 Votes
    2 Posts
    1k Views
    J
    @Eric24 It is through the ULP. I'm not sure how Pycom will work with the ULP in Python, very curious to see.
  • Always reading same battery voltage

    LoPy lopy battery adc
    24
    1 Votes
    24 Posts
    11k Views
    M
    @robert-hh Oke, will make a test program. When USB is connected it indeed has a higher voltage :'). That was another mistake/issue I had :). Thanks a lot, again!
  • LoPy ADC deadzone and isolation

    LoPy adc isolation deadzone
    2
    1 Votes
    2 Posts
    1k Views
    robert-hhR
    Hello @Yunying. Your observation is correct and matches the statements of espressif and others. The ADC should be used with 0 or 6dB attenuation, the 0-reading is at 70 mV, and at high reading it gets very non-linear. You can cope with that in your firmware, and espressif announced a compensation in a later firmware. Even then, it is by no means a high precision ADC. When using the ADC, it is recommended to feed it, like any ADC, with a low impedance, low noise Op-Amp and an input stage which filters ESD and possible destructive signals. That reduces noise too. If you need an isolated input, you have to work with opto-couplers or/and isolation amplifiers. You can also run the whole unit on battery. If you need USB connectivity, there are so called USB-Isolators, which isolate the USB device from the PC. Just look for that e.g. on EBay or Aliexpress. If you need a good quality ADC, use an external one, like the ads1115. There are isolating I2C bridges and DC/DC converters which would allow to run the ADC and input stage in a separate power domain, solving the request for isolation.
  • NTC temperature sensor

    Moved LoPy sensors lopy adc temperature ntc
    26
    0 Votes
    26 Posts
    15k Views
    robert-hhR
    @jmarcelino: Yes, Ive read that. But you would still have to compensate for the non-zero range (at least in your circuit). So at the moment and with limited requirements for precision, using the lower attenuation variants 0 and 6db and simply adding an offset would be sufficient as compensation. If you need precise values, yes, then an external ADC is anyhow recommended. B.T.W. Is there still the option to use the ADC in 10 bit mode, like by an option res = xx in the channel set-up?