Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import spidev
- import time
- import os
- # Schematic values
- resVal = 23
- ratio = 2000
- zeroVal = 1.66
- # Open SPI bus
- spi = spidev.SpiDev()
- spi.open(0,0)
- # Function to read SPI data from MCP3008 chip
- # Channel must be an integer 0-7
- def ReadChannel(channel):
- adc = spi.xfer2([1,(8+channel)<<4,0])
- data = ((adc[1]&3) << 8) + adc[2]
- return data
- # Function to convert data to voltage level,
- # rounded to specified number of decimal places.
- def ConvertVolts(data,places):
- volts = (data * 3.3) / float(1023)
- volts = round(volts,places)
- return volts
- # Define sensor channels
- sensor_channel = 0
- # Define delay between readings
- delay = 1
- # Define voltage drop
- volDif = 0
- while True:
- # Read the current sensor data
- sensor_level = ReadChannel(sensor_channel)
- sensor_volts = ConvertVolts(sensor_level,2)
- # Define
- if sensor_volts>1.66:
- volDif = sensor_volts - 1.66
- else:
- volDif = 1.66 - sensor_volts
- # Calc Amps secondary
- sensorAmpsec = (volDif / resVal) * 1000
- # Calc Amps primary
- sensorAmpsprim = (sensorAmpsec / 1000) * ratio
- # Print out results
- print "--------------------------------------------"
- print("Bits: {} ({}V)".format(sensor_level,sensor_volts))
- print("Amps: SEC - {} mA | PRIM - {} A".format(round(sensorAmpsec,2),round(sensorAmpsprim,2)))
- # Wait before repeating loop
- time.sleep(delay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement