title: I have been trying to create a csv file from python code on raspberry pi that can measure voltage, current, power, shunt voltage from ina219 sensor
body: <p>I have been trying to create a csv file from a python code that is able to track the bus Voltage, bus current, power, and shunt voltage from a INA219 sensor that is connected to the raspberry pi3. This is the code that I have been working:</p>
  <pre><code>**#Importing libraries
  import csv
  from ina219 import INA219
  from ina219 import DeviceRangeError
  SHUNT_OHMS = 0.1
  def read():
      ina = INA219(SHUNT_OHMS) #Measures the ina219
      ina.configure()
  def measuring_things():
      try:
           print(&quot;Bus Voltage: %.3f V&quot; % ina.voltage())
           print(&quot;Bus Current: %.3f mA&quot; % ina.current())
           print(&quot;Power: %.3f mW&quot; % ina.power())
           print(&quot;Shunt voltage: %.3f mV&quot; % ina.shunt_voltage())
      except DeviceRangeError as e:
      # Current out of device range with specified shunt resistor  
          print(e)
      
  f = open(&quot;SensorData.csv&quot;, &quot;w&quot;, newline=&quot;&quot;) #Gives your .csv file a name
  rc = csv.writer(f)
  rc.writerow([&quot;Distance from Sensor&quot;]) #Heading
  fields = ['Bus Voltage', 'Bus Current', 'Power', 'Shunt Voltage'] 
            
  rc.writerow([read()])
  rc.writerow(measuring_things())
  f.close()** 
  </code></pre>
  <p>I have changed the code around multiple times alongside created other codes and I keep getting an error from the ina.voltage, ina.current part no matter what I do. I keep getting an error saying:</p>
  <pre><code>============= RESTART: /home/pi/Downloads/scripts/SensorData.py =============
  Traceback (most recent call last):
    File &quot;/home/pi/Downloads/scripts/SensorData.py&quot;, line 34, in &lt;module&gt;
      rc.writerow(measuring_things())
    File &quot;/home/pi/Downloads/scripts/SensorData.py&quot;, line 16, in measuring_things
      print(&quot;Bus Voltage: %.3f V&quot; % ina.voltage())
  NameError: name 'ina' is not defined
  </code></pre>
  <p>I am getting irritating errors, they could be minor but I just feel stuck. My goal is to create a cvs file that is similar to this information :</p>
  <pre><code>============== RESTART: /home/pi/Downloads/scripts/Autogain.py ==============
  Bus Voltage: 0.876 V
  Bus Current: -0.098 mA
  Power: 0.000 mW
  Shunt voltage: -0.010 mV
  </code></pre>
  <p>This output I attached was done using python on the Raspberry pi 3 (with the INA219 sensor connected to it) by itself and not in hand converting it into a csv file.
  The Rapberry system reads and is able to sense the INA219 sensor so I am not sure what is going on.</p>