Skip to content

Commit 651d415

Browse files
committed
2 parents 00ea1db + 84ff0c7 commit 651d415

5 files changed

Lines changed: 90 additions & 92 deletions

File tree

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
# FILE: HallEffect-analogNative.py
1+
# FILE: HallEffect-analogNative.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
33
# BRIEF: An example of getting the magnetic field value from the Hall Effect sensor
44
# WORKS WITH: Hall effect sensor breakout with analog output: www.solde.red/333079
5-
# LAST UPDATED: 2025-06-10
5+
# LAST UPDATED: 2025-06-10
66

77
from HallEffect import HallEffectAnalog
88
from time import sleep
99

10-
#Initialize the sensor in native mode by giving it the board pin connected to OUT pin
11-
sensor=HallEffectAnalog(pin=34)
10+
# Initialize the sensor in native mode by giving it the board pin connected to OUT pin
11+
sensor = HallEffectAnalog(pin=34)
1212

13-
#Infinite loop
13+
# Infinite loop
1414
while 1:
15-
#Get raw voltage value on OUT pin
16-
analog=sensor.getReading()
15+
# Get raw voltage value on OUT pin
16+
analog = sensor.getReading()
1717

18-
#Get the raw voltage value converted into milli Teslas
19-
reading=sensor.getMilliTeslas()
18+
# Get the raw voltage value converted into milli Teslas
19+
reading = sensor.getMilliTeslas()
2020

21-
#Print out the readings
22-
print("ADC reading:",analog)
23-
print("Magnetic field= ",reading,"mT")
24-
#Empty row
21+
# Print out the readings
22+
print("ADC reading:", analog)
23+
print("Magnetic field= ", reading, "mT")
24+
# Empty row
2525
print()
26-
#Pause for 1 second
27-
sleep(1.0)
26+
# Pause for 1 second
27+
sleep(1.0)
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# FILE: HallEffect-analogQwiic.py
1+
# FILE: HallEffect-analogQwiic.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
33
# BRIEF: An example of getting the magnetic field value from the Hall Effect sensor
44
# WORKS WITH: Hall effect sensor breakout with analog output & easyC: www.solde.red/333082
5-
# LAST UPDATED: 2025-06-10
5+
# LAST UPDATED: 2025-06-10
66

77
from HallEffect import HallEffectAnalog
88
from time import sleep
@@ -11,26 +11,25 @@
1111
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
1212
# sensor = HallEffectAnalog(i2c)
1313

14-
#Initialize sensor over Qwiic
15-
sensor=HallEffectAnalog()
16-
17-
#You can also set a custom I2C address in the initialization:
18-
#sensor=HallEffectAnalog(address=0x31)
14+
# Initialize sensor over Qwiic
15+
sensor = HallEffectAnalog()
1916

17+
# You can also set a custom I2C address in the initialization:
18+
# sensor=HallEffectAnalog(address=0x31)
2019

2120

2221
while 1:
23-
#Get raw voltage value
24-
analog=sensor.getReading()
22+
# Get raw voltage value
23+
analog = sensor.getReading()
24+
25+
# Get the raw voltage value converted into milli Teslas
26+
reading = sensor.getMilliTeslas()
2527

26-
#Get the raw voltage value converted into milli Teslas
27-
reading=sensor.getMilliTeslas()
28-
29-
#Print out the readings
30-
print("ADC reading:",analog)
31-
print("Magnetic field= ",reading,"mT")
28+
# Print out the readings
29+
print("ADC reading:", analog)
30+
print("Magnetic field= ", reading, "mT")
3231

33-
#Empty row
32+
# Empty row
3433
print()
35-
#Pause for 1 second
36-
sleep(1.0)
34+
# Pause for 1 second
35+
sleep(1.0)
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
# FILE: HallEffect-digitalNative.py
1+
# FILE: HallEffect-digitalNative.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
33
# BRIEF: An example of detecting a magnetic field with the Hall Effect sensor
44
# WORKS WITH: Hall effect sensor breakout with digital output: www.solde.red/333080
5-
# LAST UPDATED: 2025-06-10
5+
# LAST UPDATED: 2025-06-10
66

77
from HallEffect import HallEffectDigital
88
from time import sleep
99

10-
#Initialize the sensor in native mode by giving it the board pin connected to OUT pin
11-
sensor=HallEffectDigital(pin=34)
10+
# Initialize the sensor in native mode by giving it the board pin connected to OUT pin
11+
sensor = HallEffectDigital(pin=34)
1212

13-
#Infinite loop
13+
# Infinite loop
1414
while 1:
15-
#Check if magnetic field is present
16-
reading=sensor.getReading()
17-
#If it is, print it out to inform the user
18-
if(reading):
15+
# Check if magnetic field is present
16+
reading = sensor.getReading()
17+
# If it is, print it out to inform the user
18+
if reading:
1919
print("Magnet detected!")
20-
#Pause for 1 second
21-
sleep(1.0)
20+
# Pause for 1 second
21+
sleep(1.0)
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
# FILE: HallEffect-digitalQwiic.py
1+
# FILE: HallEffect-digitalQwiic.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
33
# BRIEF: An example of detecting a magnetic field with the Hall Effect sensor
44
# WORKS WITH: Hall effect sensor breakout with digital output & easyC: www.solde.red/333081
5-
# LAST UPDATED: 2025-06-10
5+
# LAST UPDATED: 2025-06-10
66

77
from HallEffect import HallEffectDigital
88
from time import sleep
99
# If you aren't using the Qwiic connector, manually enter your I2C pins
1010
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
1111
# sensor = HallEffectDigital(i2c)
1212

13-
#Initialize sensor over I2C
14-
sensor=HallEffectDigital()
13+
# Initialize sensor over I2C
14+
sensor = HallEffectDigital()
1515

16-
#You can also set a custom I2C address in the initialization:
17-
#sensor=HallEffectDigital(address=0x31)
16+
# You can also set a custom I2C address in the initialization:
17+
# sensor=HallEffectDigital(address=0x31)
1818

19-
#Infinite loop
19+
# Infinite loop
2020
while 1:
21-
22-
#Check if magnetic field is present
23-
reading=sensor.getReading()
24-
#If it is, print it out to inform the user
25-
if(reading):
21+
# Check if magnetic field is present
22+
reading = sensor.getReading()
23+
# If it is, print it out to inform the user
24+
if reading:
2625
print("Magnet detected!")
27-
#Pause for 1 second
28-
sleep(1.0)
26+
# Pause for 1 second
27+
sleep(1.0)
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# FILE: HallEffect.py
1+
# FILE: HallEffect.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
3-
# BRIEF: A Micropython module used for the Hall Effect sensor family of products, supports both
3+
# BRIEF: A Micropython module used for the Hall Effect sensor family of products, supports both
44
# Digital and Analog versions as well as the native and Qwiic variants
5-
# LAST UPDATED: 2025-06-10
5+
# LAST UPDATED: 2025-06-10
66

77
from os import uname
88
from Qwiic import Qwiic
9-
from machine import Pin,I2C,ADC
9+
from machine import Pin, I2C, ADC
1010

1111

12-
ANALOG_READ_REG=0
12+
ANALOG_READ_REG = 0
1313

1414

1515
class HallEffectAnalog(Qwiic):
16-
def __init__(self,i2c=None,address=0x30,pin=None):
16+
def __init__(self, i2c=None, address=0x30, pin=None):
1717
"""
1818
Initializes the Hall Effect Sensor.
1919
@@ -22,23 +22,23 @@ def __init__(self,i2c=None,address=0x30,pin=None):
2222
:param pin: GPIO pin number for native mode. If provided, native mode is used.
2323
"""
2424
if uname().sysname == "esp32":
25-
self.VOLTAGE_RES=3.3
26-
self.ADC_MAX=4096
27-
self.NUM_BITS=12
25+
self.VOLTAGE_RES = 3.3
26+
self.ADC_MAX = 4096
27+
self.NUM_BITS = 12
2828
elif uname().sysname == "esp8266":
29-
self.VOLTAGE_RES=3.3
30-
self.ADC_MAX=1024
31-
self.NUM_BITS=10
29+
self.VOLTAGE_RES = 3.3
30+
self.ADC_MAX = 1024
31+
self.NUM_BITS = 10
3232
else:
33-
self.VOLTAGE_RES=5
34-
self.ADC_MAX=1024
35-
self.NUM_BITS=10
36-
33+
self.VOLTAGE_RES = 5
34+
self.ADC_MAX = 1024
35+
self.NUM_BITS = 10
36+
3737
if pin is not None:
3838
# Native mode
3939
super().__init__(i2c=None, native=True)
4040
self.pin = ADC(pin)
41-
self.native=True
41+
self.native = True
4242
self.pin.atten(ADC.ATTN_11DB)
4343
else:
4444
if i2c != None:
@@ -49,39 +49,40 @@ def __init__(self,i2c=None,address=0x30,pin=None):
4949
else:
5050
raise Exception("Board not recognized, enter I2C pins manually")
5151
super().__init__(i2c=i2c, address=address, native=False)
52-
52+
5353
def initialize_native(self):
5454
"""
5555
Setup for native GPIO input.
5656
"""
5757
# Already initialized in __init__
5858
pass
59-
59+
6060
def getReading(self) -> int:
6161
if self.native:
6262
return self.pin.read()
6363
else:
64-
data = self.read_register(ANALOG_READ_REG,2)
65-
value=data[1]<<8|data[0]
64+
data = self.read_register(ANALOG_READ_REG, 2)
65+
value = data[1] << 8 | data[0]
6666
return value
67-
67+
6868
def getMilliTeslas(self) -> float:
69-
value=self.getReading()
69+
value = self.getReading()
7070
if self.native:
7171
if uname().sysname == "esp32":
72-
if (value >= 2710):
72+
if value >= 2710:
7373
return (value - 2710.0) * (20.47 - 0.0) / (4095.0 - 2710.0) + 0.0
7474
else:
7575
return (value) * (20.47) / (2710.0) - 20.47
7676
else:
77-
return 20.47 * (self.NUM_BITS * (value / (self.ADC_MAX - 1)) / self.VOLTAGE_RES - 1)
77+
return 20.47 * (
78+
self.NUM_BITS * (value / (self.ADC_MAX - 1)) / self.VOLTAGE_RES - 1
79+
)
7880
else:
7981
return 20.47 * (10 * (value / 1023.0) / 5.0 - 1)
80-
81-
82-
82+
83+
8384
class HallEffectDigital(Qwiic):
84-
def __init__(self,i2c=None,address=0x30,pin=None):
85+
def __init__(self, i2c=None, address=0x30, pin=None):
8586
"""
8687
Initializes the Hall Effect Sensor.
8788
@@ -94,7 +95,7 @@ def __init__(self,i2c=None,address=0x30,pin=None):
9495
# Native mode
9596
super().__init__(i2c=None, native=True)
9697
self.pin = Pin(pin, Pin.IN)
97-
self.native=True
98+
self.native = True
9899
else:
99100
if i2c != None:
100101
i2c = i2c
@@ -104,18 +105,17 @@ def __init__(self,i2c=None,address=0x30,pin=None):
104105
else:
105106
raise Exception("Board not recognized, enter I2C pins manually")
106107
super().__init__(i2c=i2c, address=address, native=False)
107-
108+
108109
def initialize_native(self):
109110
"""
110111
Setup for native GPIO input.
111112
"""
112113
# Already initialized in __init__
113114
pass
114-
115+
115116
def getReading(self) -> int:
116117
if self.native:
117118
return not (self.pin.value())
118119
else:
119-
data = self.read_register(0,2)
120+
data = self.read_register(0, 2)
120121
return not (data[1])
121-

0 commit comments

Comments
 (0)