Advertisement
microrobotics

INA3221 3 Channel Current sensor with I2C

Mar 30th, 2023
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The INA3221 is a 3 channel current sensor with I2C interface. Here's an example code in Arduino that reads the current from the sensor:
  3.  
  4. Note: This code assumes that the INA3221 sensor is connected to the I2C bus of the Arduino board. If you're using a different interface, you'll need to modify the Wire library calls accordingly. Also, the calibration values used in the code are for the default configuration of the INA3221, which has a maximum voltage of 32V and maximum current of 2A. If you're using a different configuration, you'll need to set the calibration values accordingly using the appropriate setCalibration method. Finally, note that the INA3221 has a maximum input voltage of 36V, so be sure to use appropriate voltage dividers or other means to ensure that the voltage applied to the sensor does not exceed this limit.
  5. */
  6.  
  7. #include <Wire.h>
  8. #include <Adafruit_INA3221.h>
  9.  
  10. Adafruit_INA3221 ina3221;
  11.  
  12. void setup() {
  13.   Serial.begin(9600);
  14.  
  15.   if (!ina3221.begin()) {  // initialize the sensor
  16.     Serial.println("Could not find INA3221 sensor!");
  17.     while (1);
  18.   }
  19.  
  20.   ina3221.setCalibration_32V_2A();  // set the calibration values for the sensor
  21. }
  22.  
  23. void loop() {
  24.   // read the current from each channel of the sensor
  25.   float channel1_current = ina3221.getCurrent_mA(1);
  26.   float channel2_current = ina3221.getCurrent_mA(2);
  27.   float channel3_current = ina3221.getCurrent_mA(3);
  28.  
  29.   // print the current for each channel
  30.   Serial.print("Channel 1 Current: ");
  31.   Serial.print(channel1_current);
  32.   Serial.println(" mA");
  33.   Serial.print("Channel 2 Current: ");
  34.   Serial.print(channel2_current);
  35.   Serial.println(" mA");
  36.   Serial.print("Channel 3 Current: ");
  37.   Serial.print(channel3_current);
  38.   Serial.println(" mA");
  39.  
  40.   // delay before reading from the sensor again
  41.   delay(1000);
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement