Skip to content

Commit 260af76

Browse files
committed
update to 1.0.7
1 parent adf3654 commit 260af76

File tree

11 files changed

+1472
-0
lines changed

11 files changed

+1472
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Eduponics/__pycache__/*
22
dist/*
33
micropython_eduponics.egg-info/*
4+
.DS_Store

eduponics/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "1.0.7"

eduponics/ads1x15.py

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2016 Radomir Dopieralski (@deshipu),
4+
# 2017 Robert Hammelrath (@robert-hh),
5+
# 2020 STEMinds (@STEMinds)
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
25+
import utime as time
26+
from eduponics import mcp23017
27+
28+
_REGISTER_CONVERT = const(0x00)
29+
_REGISTER_CONFIG = const(0x01)
30+
_REGISTER_LOWTHRESH = const(0x02)
31+
_REGISTER_HITHRESH = const(0x03)
32+
33+
_OS_SINGLE = const(0x8000) # Write: Set to start a single-conversion
34+
_OS_NOTBUSY = const(0x8000) # Read: Bit=1 when no conversion is in progress
35+
36+
_MUX_DIFF_0_1 = const(0x0000) # Differential P = AIN0, N = AIN1 (default)
37+
_MUX_DIFF_0_3 = const(0x1000) # Differential P = AIN0, N = AIN3
38+
_MUX_DIFF_1_3 = const(0x2000) # Differential P = AIN1, N = AIN3
39+
_MUX_DIFF_2_3 = const(0x3000) # Differential P = AIN2, N = AIN3
40+
_MUX_SINGLE_0 = const(0x4000) # Single-ended AIN0
41+
_MUX_SINGLE_1 = const(0x5000) # Single-ended AIN1
42+
_MUX_SINGLE_2 = const(0x6000) # Single-ended AIN2
43+
_MUX_SINGLE_3 = const(0x7000) # Single-ended AIN3
44+
45+
_PGA_6_144V = const(0x0000) # +/-6.144V range = Gain 2/3
46+
_PGA_4_096V = const(0x0200) # +/-4.096V range = Gain 1
47+
_PGA_2_048V = const(0x0400) # +/-2.048V range = Gain 2 (default)
48+
_PGA_1_024V = const(0x0600) # +/-1.024V range = Gain 4
49+
_PGA_0_512V = const(0x0800) # +/-0.512V range = Gain 8
50+
_PGA_0_256V = const(0x0A00) # +/-0.256V range = Gain 16
51+
52+
_MODE_CONTIN = const(0x0000) # Continuous conversion mode
53+
_MODE_SINGLE = const(0x0100) # Power-down single-shot mode (default)
54+
55+
_DR_128SPS = const(0x0000) # 128 /8 samples per second
56+
_DR_250SPS = const(0x0020) # 250 /16 samples per second
57+
_DR_490SPS = const(0x0040) # 490 /32 samples per second
58+
_DR_920SPS = const(0x0060) # 920 /64 samples per second
59+
_DR_1600SPS = const(0x0080) # 1600/128 samples per second (default)
60+
_DR_2400SPS = const(0x00A0) # 2400/250 samples per second
61+
_DR_3300SPS = const(0x00C0) # 3300/475 samples per second
62+
_DR_860SPS = const(0x00E0) # - /860 samples per Second
63+
64+
_CMODE_TRAD = const(0x0000) # Traditional comparator with hysteresis (default)
65+
66+
_CPOL_ACTVLOW = const(0x0000) # ALERT/RDY pin is low when active (default)
67+
68+
_CLAT_NONLAT = const(0x0000) # Non-latching comparator (default)
69+
_CLAT_LATCH = const(0x0004) # Latching comparator
70+
71+
_CQUE_1CONV = const(0x0000) # Assert ALERT/RDY after one conversions
72+
# Disable the comparator and put ALERT/RDY in high state (default)
73+
_CQUE_NONE = const(0x0003)
74+
75+
_GAINS = (
76+
_PGA_6_144V, # 2/3x
77+
_PGA_4_096V, # 1x
78+
_PGA_2_048V, # 2x
79+
_PGA_1_024V, # 4x
80+
_PGA_0_512V, # 8x
81+
_PGA_0_256V # 16x
82+
)
83+
84+
_GAINS_V = (
85+
6.144, # 2/3x
86+
4.096, # 1x
87+
2.048, # 2x
88+
1.024, # 4x
89+
0.512, # 8x
90+
0.256 # 16x
91+
)
92+
93+
_CHANNELS = {
94+
(0, None): _MUX_SINGLE_0,
95+
(1, None): _MUX_SINGLE_1,
96+
(2, None): _MUX_SINGLE_2,
97+
(3, None): _MUX_SINGLE_3,
98+
(0, 1): _MUX_DIFF_0_1,
99+
(0, 3): _MUX_DIFF_0_3,
100+
(1, 3): _MUX_DIFF_1_3,
101+
(2, 3): _MUX_DIFF_2_3,
102+
}
103+
104+
_RATES = (
105+
_DR_128SPS, # 128/8 samples per second
106+
_DR_250SPS, # 250/16 samples per second
107+
_DR_490SPS, # 490/32 samples per second
108+
_DR_920SPS, # 920/64 samples per second
109+
_DR_1600SPS, # 1600/128 samples per second (default)
110+
_DR_2400SPS, # 2400/250 samples per second
111+
_DR_3300SPS, # 3300/475 samples per second
112+
_DR_860SPS # - /860 samples per Second
113+
)
114+
115+
116+
class ADS1115:
117+
def __init__(self, i2c, address=0x48, gain=1, mcp_address=0x20):
118+
self.i2c = i2c
119+
self.address = address
120+
self.mcp_address = mcp_address
121+
self.gain = gain
122+
self.temp2 = bytearray(2)
123+
# define MCP for activating MOSFET pins
124+
self.mcp = mcp23017.MCP23017(i2c=self.i2c, address=mcp_address)
125+
# define all the pins for the mosfets
126+
self.mcp_pins_sheet = {
127+
0:8,
128+
1:9,
129+
2:10,
130+
3:11
131+
}
132+
133+
def _write_register(self, register, value):
134+
self.temp2[0] = value >> 8
135+
self.temp2[1] = value & 0xff
136+
self.i2c.writeto_mem(self.address, register, self.temp2)
137+
138+
def _read_register(self, register):
139+
self.i2c.readfrom_mem_into(self.address, register, self.temp2)
140+
return (self.temp2[0] << 8) | self.temp2[1]
141+
142+
def raw_to_v(self, raw):
143+
v_p_b = _GAINS_V[self.gain] / 32767
144+
return raw * v_p_b
145+
146+
def set_conv(self, rate=4, channel1=0, channel2=None):
147+
"""Set mode for read_rev"""
148+
self.mode = (_CQUE_NONE | _CLAT_NONLAT |
149+
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
150+
_MODE_SINGLE | _OS_SINGLE | _GAINS[self.gain] |
151+
_CHANNELS[(channel1, channel2)])
152+
153+
def read_raw(self, rate=4, channel1=0, channel2=None):
154+
"""Read voltage between a channel and GND.
155+
Time depends on conversion rate."""
156+
self._write_register(_REGISTER_CONFIG, (_CQUE_NONE | _CLAT_NONLAT |
157+
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
158+
_MODE_SINGLE | _OS_SINGLE | _GAINS[self.gain] |
159+
_CHANNELS[(channel1, channel2)]))
160+
while not self._read_register(_REGISTER_CONFIG) & _OS_NOTBUSY:
161+
time.sleep_ms(1)
162+
res = self._read_register(_REGISTER_CONVERT)
163+
return res if res < 32768 else res - 65536
164+
165+
def read_rev(self):
166+
"""Read voltage between a channel and GND. and then start
167+
the next conversion."""
168+
res = self._read_register(_REGISTER_CONVERT)
169+
self._write_register(_REGISTER_CONFIG, self.mode)
170+
return res if res < 32768 else res - 65536
171+
172+
def alert_start(self, rate=4, channel1=0, channel2=None,
173+
threshold_high=0x4000, threshold_low=0, latched=False) :
174+
"""Start continuous measurement, set ALERT pin on threshold."""
175+
self._write_register(_REGISTER_LOWTHRESH, threshold_low)
176+
self._write_register(_REGISTER_HITHRESH, threshold_high)
177+
self._write_register(_REGISTER_CONFIG, _CQUE_1CONV |
178+
_CLAT_LATCH if latched else _CLAT_NONLAT |
179+
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
180+
_MODE_CONTIN | _GAINS[self.gain] |
181+
_CHANNELS[(channel1, channel2)])
182+
183+
def conversion_start(self, rate=4, channel1=0, channel2=None):
184+
"""Start continuous measurement, trigger on ALERT/RDY pin."""
185+
self._write_register(_REGISTER_LOWTHRESH, 0)
186+
self._write_register(_REGISTER_HITHRESH, 0x8000)
187+
self._write_register(_REGISTER_CONFIG, _CQUE_1CONV | _CLAT_NONLAT |
188+
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
189+
_MODE_CONTIN | _GAINS[self.gain] |
190+
_CHANNELS[(channel1, channel2)])
191+
192+
def alert_read(self):
193+
"""Get the last reading from the continuous measurement."""
194+
res = self._read_register(_REGISTER_CONVERT)
195+
return res if res < 32768 else res - 65536
196+
197+
def read(self,pin):
198+
# activate the mosfet
199+
self.mcp.pin(self.mcp_pins_sheet[pin], mode=0, value=1)
200+
time.sleep(0.1)
201+
# read the data
202+
raw = self.read_raw(channel1=pin)
203+
voltage = self.raw_to_v(raw)
204+
# deactivate mosfet after use
205+
self.mcp.pin(self.mcp_pins_sheet[pin], mode=0, value=0)
206+
return {"raw":raw,"voltage":voltage}

eduponics/at24c02.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
MicroPython TinyRTC I2C Module, DS1307 RTC + AT24C32N EEPROM
3+
https://github.com/mcauser/micropython-tinyrtc
4+
MIT License
5+
Copyright (c) 2018 Mike Causer
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
"""
22+
23+
# AT24C32A, 32K (32768 kbit / 4 KB), 128 pages, 32 bytes per page, i2c addr 0x50
24+
import machine
25+
import time
26+
27+
class AT24C32N(object):
28+
"""Driver for the AT24C32N 32K EEPROM."""
29+
30+
def __init__(self, i2c, i2c_addr=0x50, pages=128, bpp=32):
31+
self.i2c = i2c
32+
self.i2c_addr = i2c_addr
33+
self.pages = pages
34+
self.bpp = bpp # bytes per page
35+
36+
def capacity(self):
37+
"""Storage capacity in bytes"""
38+
return self.pages * self.bpp
39+
40+
def read(self, addr, nbytes):
41+
"""Read one or more bytes from the EEPROM starting from a specific address"""
42+
return self.i2c.readfrom_mem(self.i2c_addr, addr, nbytes, addrsize=16)
43+
44+
def write(self, addr, buf):
45+
"""Write one or more bytes to the EEPROM starting from a specific address"""
46+
offset = addr % self.bpp
47+
partial = 0
48+
# partial page write
49+
if offset > 0:
50+
partial = self.bpp - offset
51+
self.i2c.writeto_mem(self.i2c_addr, addr, buf[0:partial], addrsize=16)
52+
time.sleep_ms(5)
53+
addr += partial
54+
# full page write
55+
for i in range(partial, len(buf), self.bpp):
56+
self.i2c.writeto_mem(self.i2c_addr, addr+i-partial, buf[i:i+self.bpp], addrsize=16)
57+
time.sleep_ms(5)

eduponics/bh1750.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
MicroPython BH1750 light sensor module
3+
https://github.com/STEMinds/eduponics-mini
4+
MIT License
5+
Copyright (c) 2020 STEMinds
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
"""
24+
25+
import machine
26+
import time
27+
28+
class BH1750():
29+
30+
def __init__(self):
31+
32+
# Define some constants from the datasheet
33+
34+
self.DEVICE = 0x5c # Default device I2C address
35+
36+
self.POWER_DOWN = 0x00 # No active state
37+
self.POWER_ON = 0x01 # Power on
38+
self.RESET = 0x07 # Reset data register value
39+
40+
# Start measurement at 4lx resolution. Time typically 16ms.
41+
self.CONTINUOUS_LOW_RES_MODE = 0x13
42+
# Start measurement at 1lx resolution. Time typically 120ms
43+
self.CONTINUOUS_HIGH_RES_MODE_1 = 0x10
44+
# Start measurement at 0.5lx resolution. Time typically 120ms
45+
self.CONTINUOUS_HIGH_RES_MODE_2 = 0x11
46+
# Start measurement at 1lx resolution. Time typically 120ms
47+
# Device is automatically set to Power Down after measurement.
48+
self.ONE_TIME_HIGH_RES_MODE_1 = 0x20
49+
# Start measurement at 0.5lx resolution. Time typically 120ms
50+
# Device is automatically set to Power Down after measurement.
51+
self.ONE_TIME_HIGH_RES_MODE_2 = 0x21
52+
# Start measurement at 1lx resolution. Time typically 120ms
53+
# Device is automatically set to Power Down after measurement.
54+
self.ONE_TIME_LOW_RES_MODE = 0x23
55+
# setup I2C
56+
self.i2c = machine.I2C(scl=machine.Pin(15), sda=machine.Pin(4))
57+
58+
def convertToNumber(self, data):
59+
60+
# Simple function to convert 2 bytes of data
61+
# into a decimal number
62+
return ((data[1] + (256 * data[0])) / 1.2)
63+
64+
def readLight(self):
65+
66+
data = self.i2c.readfrom_mem(self.DEVICE,self.ONE_TIME_HIGH_RES_MODE_1,2)
67+
return self.convertToNumber(data)

0 commit comments

Comments
 (0)