Welcome! Share code as fast as possible.
- Use the language picker to change the language manually.
- The previous link will get modified. You can then share this new link with others.
- Visit
https://code-dump.vercel.app/<extension>
- For example, to create a link which for a JavaScript code you will visit
https://code-dump.vercel.app/js
Any link generated does not have an expiry.
GitHub
MLX90393 not plugged in:
>>> %Run 'tca95 - mlx90393 singlesensor.py'
Traceback (most recent call last):
File "/home/pi/mussel_prototype0/tca95 - mlx90393 singlesensor.py", line 68, in <module>
I2C_setup_readadc(0x70,0)
File "/home/pi/mussel_prototype0/tca95 - mlx90393 singlesensor.py", line 19, in I2C_setup_readadc
bus.write_i2c_block_data(mlx90393_address, 0x60, config)
OSError: [Errno 121] Remote I/O error
MLX90393 plugged in:
>>> %Run 'tca95 - mlx90393 singlesensor.py'
Traceback (most recent call last):
File "/home/pi/mussel_prototype0/tca95 - mlx90393 singlesensor.py", line 68, in <module>
I2C_setup_readadc(0x70,0)
File "/home/pi/mussel_prototype0/tca95 - mlx90393 singlesensor.py", line 40, in I2C_setup_readadc
data = bus.read_byte(mlx90393_address)
OSError: [Errno 121] Remote I/O error
>>>
Code example:
#!/usr/bin/python
# intended for import to change channel of TCA9548A
# import then call I2C_setup(multiplexer_addr,multiplexer_channel)
import smbus
import time
import sys
mlx90393_address = 0x10
#channel_array_multiplexer=[0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000]
channel_array_multiplexer=[0b00000001]
def I2C_setup_readadc(multiplexer,i2c_channel_setup):
bus = smbus.SMBus(1)
bus.write_byte(multiplexer,channel_array_multiplexer[i2c_channel_setup])
time.sleep(0.01)
#print("TCA9548A I2C channel status:", bin(bus.read_byte(multiplexer)))
config = [0x00, 0x5C, 0x00]
#bus.write_i2c_block_data(0x0C, 0x60, config)
bus.write_i2c_block_data(mlx90393_address, 0x60, config)
# Read data back, 1 byte
# Status byte
data = bus.read_byte(mlx90393_address)
# MLX90393 address, 0x0C(12)
# Select write register command, 0x60(96)
# AH = 0x02, AL = 0xB4, RES for magnetic measurement = 0, Address register (0x02 << 2)
config = [0x02, 0xB4, 0x08]
bus.write_i2c_block_data(mlx90393_address, 0x60, config)
# Read data back, 1 byte
# Status byte
data = bus.read_byte(mlx90393_address)
# MLX90393 address, 0x0C(12)
# Start single meaurement mode, X, Y, Z-Axis enabled
bus.write_byte(mlx90393_address, 0x3E)
# Read data back, 1 byte
# Status byte
data = bus.read_byte(mlx90393_address)
time.sleep(0.01)
# MLX90393 address, 0x0C(12)
# Read data back from 0x4E(78), 7 bytes
# Status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
data = bus.read_i2c_block_data(mlx90393_address, 0x4E, 7)
# Convert the data
xMag = data[1] * 256 + data[2]
if xMag > 32767 :
xMag -= 65536
yMag = data[3] * 256 + data[4]
if yMag > 32767 :
yMag -= 65536
zMag = data[5] * 256 + data[6]
if zMag > 32767 :
zMag -= 65536
# Output data to screen
print("Magnetic Field in X-Axis : %d" %xMag)
print("Magnetic Field in Y-Axis : %d" %yMag)
print("Magnetic Field in Z-Axis : %d" %zMag)
while True:
I2C_setup_readadc(0x70,0)
time.sleep(2)