The INA238 Python Library provides a user-friendly interface for interacting with the INA238 sensor over I2C. The library allows for easy configuration and data acquisition, supporting various settings for voltage range, ADC resolution, and averaging samples.
If you have any questions, please contact the Repo-Owner.
Luca-Timo
Follow these steps to get the code up and running on your system:
-
Clone the repository:
git clone https://github.com/yourusername/INA238-python-library.git cd INA238-python-library -
Install the required Python packages:
pip install smbus
- Python 3.x
- smbus library for I2C communication
Here's an example of how to use the library:
from ina238 import INA238, Mode, ConversionTime, Samples, ADCRange
# Initialize the sensor
sensor = INA238(bus_num=1, addr=0x40, max_voltage=32, shunt_resistance=0.1, max_current=3.2)
# Configure the sensor
sensor.configure(
voltage_range=ADCRange.HIGH,
mode=Mode.CONTINUOUS_VBUS_VSHUNT_DIETEMP,
bus_adc=ConversionTime.T_1052_US,
shunt_adc=ConversionTime.T_1052_US,
avg=Samples.AVG_128
)
# Read values
print("Bus Voltage: {:.2f} V".format(sensor.voltage()))
print("Supply Voltage: {:.2f} V".format(sensor.supply_voltage()))
print("Shunt Voltage: {:.2f} mV".format(sensor.shunt_voltage()))
print("Current: {:.2f} mA".format(sensor.current()))
print("Power: {:.2f} mW".format(sensor.power()))
print("Temperature: {:.2f} °C".format(sensor.get_temperature()))sensor = INA238(bus_num=1, addr=0x40, max_voltage=32, shunt_resistance=0.1, max_current=3.2)bus_num: The I2C bus number (default is 10).addr: The I2C address of the INA238 sensor (default is 0x40).max_voltage: The maximum expected bus voltage (default is 32V).shunt_resistance: The shunt resistor value in ohms (default is 0.1 ohms).max_current: The maximum expected current in amps (default is 3.2A).
sensor.configure(
voltage_range=ADCRange.HIGH,
mode=Mode.CONTINUOUS_VBUS_VSHUNT_DIETEMP,
bus_adc=ConversionTime.T_1052_US,
shunt_adc=ConversionTime.T_1052_US,
avg=Samples.AVG_128
)voltage_range: The ADC range setting (ADCRange.HIGHorADCRange.LOW).mode: The operating mode (Mode.CONTINUOUS_VBUS_VSHUNT_DIETEMPor otherModevalues).bus_adc: The ADC conversion time for the bus voltage (ConversionTime.T_1052_USor otherConversionTimevalues).shunt_adc: The ADC conversion time for the shunt voltage (ConversionTime.T_1052_USor otherConversionTimevalues).avg: The ADC sample averaging count (Samples.AVG_128or otherSamplesvalues).
bus_voltage = sensor.voltage()
supply_voltage = sensor.supply_voltage()
shunt_voltage = sensor.shunt_voltage()
current = sensor.current()
power = sensor.power()
temperature = sensor.get_temperature()voltage(): Returns the bus voltage in volts (V).supply_voltage(): Returns the supply voltage (bus voltage + shunt voltage) in volts (V).shunt_voltage(): Returns the shunt voltage in millivolts (mV).current(): Returns the current in milliamps (mA).power(): Returns the power consumption in milliwatts (mW).get_temperature(): Returns the temperature in degrees Celsius (°C).
The library raises a DeviceRangeError if there is an overflow condition when reading the current or power values. Ensure to handle this exception in your code:
try:
current = sensor.current()
power = sensor.power()
except DeviceRangeError as e:
print(f"Error: {e}")To get the device ID:
device_id = sensor.get_device_id()
print(f"Device ID: {device_id}")To build and test the library, follow these steps:
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install smbus
-
Run tests: Ensure that the INA238 sensor is connected to your system correctly and run the example script to verify the functionality.
After a release, new contributions are only allowed in new branches. After the modifications have been checked by a second person, a new release will be created.
We welcome contributions from everyone. To contribute to this repository, follow these guidelines:
-
Fork the repository.
-
Create a new branch for your changes:
git checkout -b my-feature-branch
-
Make your changes and commit them with clear messages:
git commit -m "Description of my changes" -
Push your changes to your forked repository:
git push origin my-feature-branch
-
Create a pull request to merge your changes into the main repository.
If you notice any errors and are unable to correct them, please create an issue in the GitHub repository.
For more information on creating good readme files, refer to the following guidelines. You can also seek inspiration from the below readme files:
- Add more configuration options.
- Improve error handling and documentation.