Skip to content

Feat: multi-sample averaging and 50Hz mains filtering #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 15, 2022

Conversation

eirinnm
Copy link
Contributor

@eirinnm eirinnm commented Aug 3, 2022

This PR implements two features from the datasheet:

  • On-chip multi-sample averaging (1,2,4,8,16 samples). Default is 1 (no averaging). Using on-chip averaging is much faster than retrieving individual samples and averaging them in user code.
  • 50Hz mains frequency notch filter. Default is off (will filter 60Hz instead). Missing feature: 50/60Hz noise filtering #18

@tekktrik tekktrik linked an issue Aug 3, 2022 that may be closed by this pull request
@ladyada
Copy link
Member

ladyada commented Aug 3, 2022

hi please create functions to set/get the freq filter (none, 50, 60?) and oversampling rather than setting once in init :)

@eirinnm eirinnm force-pushed the selectable_mains_filtering branch from 09df6a1 to aa5240c Compare August 5, 2022 17:46
@eirinnm
Copy link
Contributor Author

eirinnm commented Aug 5, 2022

Thanks for the feedback! I've created functions set_sampling and select_mains_filtering so that these options can be changed after init. For most users they don't have to be called, because the chip has sane defaults (single sampling and 60Hz filter).

@caternuson
Copy link
Contributor

Code logic looks generally OK. But instead of actual function functions, CP lib style is to use Python property feature:
https://docs.circuitpython.org/en/latest/docs/design_guide.html#getters-setters

Look at the temperature_thresholds property in this library for an example.

With that form, the following syntax is how they would get used in user code to set:

thermocouple.averaging = 16
thermocouple.noise_rejection = True

and to query (get):

print("Averaging =", thermocouple.averaging)
print("Noise Rejection =", thermocouple.noise_rejection)

In the above example code snippets, averaging and noise_rejection are the suggested property names, similar to temperature_thresholds.

@eirinnm
Copy link
Contributor Author

eirinnm commented Aug 8, 2022

Yes I see. Thanks for the advice! I've implemented it using property getter/setters now.

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One spelling fix.

@caternuson
Copy link
Contributor

LGTM. Nice job switching to property approach!

Functionally tested, without a TC connected, on Itsy M0:

Adafruit CircuitPython 7.3.2 on 2022-07-20; Adafruit ItsyBitsy M0 Express with samd21g18
>>> import board
>>> import adafruit_max31856
>>> import digitalio
>>> cs = digitalio.DigitalInOut(board.D10)
>>> cs.switch_to_output()
>>> max = adafruit_max31856.MAX31856(board.SPI(), cs)
>>> max.reference_temperature
24.375
>>> max.averaging
1
>>> max.averaging = 16
>>> max.averaging
16
>>> max.averaging = 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_max31856.py", line 190, in averaging
ValueError: Num_samples must be one of 1,2,4,8,16
>>> max.noise_rejection
60
>>> max.noise_rejection = 50
>>> max.noise_rejection
50
>>> max.noise_rejection = 23
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_max31856.py", line 218, in noise_rejection
ValueError: Frequency must be 50 or 60
>>>  

Co-authored-by: Dan Halbert <[email protected]>
@eirinnm
Copy link
Contributor Author

eirinnm commented Aug 9, 2022

Thanks all, I've enjoyed contributing to this library.

@eirinnm eirinnm requested a review from dhalbert August 10, 2022 13:40
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, verified the open change requested was completed.

Thanks for the improvement @eirinnm

@FoamyGuy FoamyGuy merged commit 825a0f7 into adafruit:main Aug 15, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Aug 16, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground to 5.2.7 from 5.2.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_CircuitPlayground#120 from dhalbert/no-pwmout-for-pulseout

Updating https://github.com/adafruit/Adafruit_CircuitPython_IRRemote to 4.1.8 from 4.1.7:
  > Merge pull request adafruit/Adafruit_CircuitPython_IRRemote#54 from dhalbert/no-pwmout

Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX31856 to 0.11.0 from 0.10.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_MAX31856#20 from eirinnm/selectable_mains_filtering

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 6.2.6 from 6.2.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#123 from dhalbert/no-auto-brightness

Updating https://github.com/adafruit/Adafruit_CircuitPython_TFmini to 1.2.12 from 1.2.11:
  > Merge pull request adafruit/Adafruit_CircuitPython_TFmini#13 from okennedy/patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_NTP to 3.0.4 from 3.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_NTP#22 from regulatre/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_PortalBase to 1.14.0 from 1.13.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_PortalBase#78 from colonwq/is_connected

Updating https://github.com/adafruit/Adafruit_CircuitPython_PYOA to 2.5.9 from 2.5.8:
  > Merge pull request adafruit/Adafruit_CircuitPython_PYOA#36 from dhalbert/no-auto-brightness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing feature: 50/60Hz noise filtering
5 participants