def __init__(self, channel, label, unit, min_value, max_value, min_threshold, max_threshold): self.adc = MCP3208(channel) self.channel = channel self.min_value = min_value self.max_value = max_value self.label = label self.unit = unit self.min_threshold = min_threshold self.max_threshold = max_threshold
def getdata(mlvol, tlength, dev): if not dev: from gpiozero import MCP3208 import time import numpy as np #initialise plotting parameters tint = 0 tplot = [] voltarray = [] plt.ion() time_elapsed = 0 initial_time = time.time() print("Running program. Please wait...") #for i in range(1,50): while time_elapsed <= tlength: if dev: voltage = random.uniform(0, 1) else: adc = MCP3208(channel=1) voltage = 3.3 * adc.value time_elapsed = time.time() - initial_time tint += time_elapsed tplot.append(tint) voltarray.append(voltage) print(voltage) plot_thist(tplot, voltarray, 0, tlength) plt.pause(0.05) with open('data/' + str(filetime) + '.csv', 'w') as f: writer = csv.writer(f, delimiter='\t') writer.writerows(zip(tplot, voltarray)) voltavg = sum(voltarray) / float(len(voltarray)) print("--------------------------------------------") print("Mean Voltage: {}V".format(voltavg)) #voltsd = np.std(voltarray) #print("Standard Deviation: {}V".format(voltsd)) plot_thist(tplot, voltarray, voltavg, tlength) return voltavg
def __init__(self, channel, sample_count=5, sample_delay=0.1, spi_args=None): if spi_args is None: spi_args = Thermistor.__DEFAULT_SPI_ARGS try: if os.getenv('GPIOZERO_PIN_FACTORY') != 'mock': self.__ic = MCP3208(channel=channel, differential=False, **spi_args) else: self.__ic = None except IOError as _: self.__ic = None self.__sample_count = sample_count self.__sample_delay = sample_delay
def get_data(mlvol, tlength, dev): import numpy as np # ### runs program to get data from ldr - stores data as array and averages. ### # #initialise plotting parameters tint = 0 tplot = [] voltarray = [] #plt.ion() #initialise time time_elapsed = 0 initial_time = time.time() print("Running program. Please wait...") #for i in range(1,50): while time_elapsed <= tlength: if dev: voltage = random.uniform(0, 1) else: #gets data from adc - calculating voltage from ldr adc = MCP3208(channel=1) voltage = 3.3 * adc.value #store and plot time_elapsed = time.time() - initial_time tint += time_elapsed tplot.append(tint) voltarray.append(voltage) #calculate average voltage across runtime, and print to console voltavg = sum(voltarray) / float(len(voltarray)) print("--------------------------------------------") print("Mean Voltage: {}V".format(voltavg)) #voltsd = np.std(voltarray) #print("Standard Deviation: {}V".format(voltsd)) #plots time history and sets filename filename = 'public/plots/interim.png' plot_thist(tplot, voltarray, voltavg, tlength, filename) #formats and returns average voltage to get_data function return "{:.4f}".format(voltavg)
def ReadMCP3208(): try: print("\nReading MCP3208......\n") dataString = '' Channel = [] value = [] returnFloatVal = '' print("Channel Available: ", channel_available) for count in range(0, channel_available): Scale = 'scale' + str(count + 1) Offset = 'offset' + str(count + 1) try: Channel.append( MCP3208(channel=count, clock_pin=ClockPin, mosi_pin=MosiPin, select_pin=SelectPin)) #print(Scale) #print(mcp3208[Scale]) #print(Channel[count].value) FloatValue = round( float(((Channel[count].value * float( (mcp3208['CalibrationFactor']))) - float(mcp3208[Offset])) * float(mcp3208[Scale])), 2) except Exception as gpioErr: print(gpioErr) FloatValue = 0.00 returnFloatVal = returnFloatVal + str(FloatValue) + ',' decimalPlace = str(FloatValue)[::-1].find('.') value.append(int(FloatValue * 10)) #print("Value of Channel no {} is" .format(value[count])) dataString = dataString + (str(value[count])) + ',' #print("Count Channel: ",count) #print(dataString) print("DataString of MCP3208 = " + dataString + '\n') return dataString, returnFloatVal except Exception as errADC: print(errADC) dataString = '' returnFloatVal = '' return dataString, returnFloatVal
def getdata(mlvol, tlength, dev): import time import numpy as np #initialise plotting parameters tint = 0 tplot = [] voltarray = [] plt.ion() time_elapsed = 0 initial_time = time.time() print("Running program. Please wait...") #for i in range(1,50): while time_elapsed <= tlength: if dev: voltage = random.uniform(0, 1) else: adc = MCP3208(channel=1) voltage = 3.3 * adc.value time_elapsed = time.time() - initial_time tint += time_elapsed tplot.append(tint) voltarray.append(voltage) voltavg = sum(voltarray) / float(len(voltarray)) print("--------------------------------------------") print("Mean Voltage: {}V".format(voltavg)) #voltsd = np.std(voltarray) #print("Standard Deviation: {}V".format(voltsd)) plot_thist(tplot, voltarray, voltavg, tlength) return voltavg
# -*- coding: utf-8 -*- #!/usr/bin/env python # Created Daichi Hayashi # Yonago Institute of Technology # Copyright ©️ 2017 Daichi Hayashi. All rights reserved. # move 3 fingers following to proximity sensors # ========== import ========== import pigpio from gpiozero import MCP3208 import time from datetime import datetime adc0 = MCP3208(channel = 0) adc1 = MCP3208(channel = 1) adc2 = MCP3208(channel = 2) Vref = 5.05 # reference voltage of MCP3208 pwm = pigpio.pi() # ========== constants ========== fing0Pin = 15 fing1Pin = 18 fing2Pin = 23 # Threshold distance to stop motor[mm] stopDst = 20.0 # start distance of control[mm] maxDst = 100.0
from gpiozero import MCP3208 import time import RPi.GPIO as GPIO import Adafruit_DHT import urllib.request #mcp setup adc1 = MCP3208(0) adc2 = MCP3208(1) # water setup MOTER_A_A1=5 MOTER_A_B1=6 GPIO.setmode(GPIO.BCM) GPIO.setup(MOTER_A_A1, GPIO.OUT) GPIO.setup(MOTER_A_B1, GPIO.OUT) MOTER_A_A1_PWM=GPIO.PWM(MOTER_A_A1, 20) MOTER_A_A1_PWM.start(0) GPIO.output(MOTER_A_B1,GPIO.LOW) #temp sensor = Adafruit_DHT.DHT11 pin = 4 humidity,temperature = Adafruit_DHT.read_retry(sensor,pin) while(True):
# #///////////////////////////////////////////////////////////////// # Startup #///////////////////////////////////////////////////////////////// # # MCP3208 A/D Startup #//////////////////////////////////////////////////////////////// # MCP3008(channel=0, clock_pin=11, mosi_pin=10, miso_pin=9, select_pin=8) # 8 channel A/D microchip # usually Chip Select is not connected to pin 22 but to pin 8 or 7 # if new sensor board is made, change that pin to new chip select connection # do not change mosi_pin # do not change clock pin # select_pin is same for a single A/D chip, so keep same for all channels # One input is converted at a time ch0=MCP3208(channel=0, clock_pin=11, mosi_pin=10, miso_pin=9, select_pin=22) ch4=MCP3208(channel=4, clock_pin=11, mosi_pin=10, miso_pin=9, select_pin=22) # interval for reading MCP A/D converter, needs to be same or slower than IMU rate # in seconds AmpHour = 0.0 pressureTorpedo = 0.0 # poll_interval = 100 print("Recommended Poll Interval: %dmS\n" % poll_interval) # # ROS Startup # ////////////////////////////////////////////////////////////////////// pubAmpHour = rospy.Publisher('AmpHour', Float64, queue_size=10) pubPressureTorpedo = rospy.Publisher('pressureTorpedo`', Float64, queue_size=10)
from time import sleep import matplotlib.pyplot as plt #initialise LED red_led = LED(17) #initialise plotting parameters tint = 0 tplot = [] lplot = [] plt.ion() delay = 0.05 while True: tmp = MCP3208(channel=7) temperature = (tmp.value * 3.3 - 0.5) * 100 if (temperature >= 25): red_led.on() else: red_led.off() # Print out results print("--------------------------------------------") print("Temp: {}C".format(temperature)) tint += delay tplot.append(tint) lplot.append(temperature)
import time #Outputting Hello World to the LCD thelcd = I2C_LCD_driver.lcd() thelcd.lcd_display_string("Hello World!",1, 1) #Sleep for 10 seconds then clear time.sleep(10) thelcd.lcd_clear() #part 2 #program to read the values from channel 0 through channel three and display the values on the LCD from gpiozero import MCP3208 import I2C_LCD_driver import time #Defining the channels to be used ch0 = MCP3208(channel = 0, differential = False, max_voltage = 3.3) ch1 = MCP3208(channel = 1, differential = False, max_voltage = 3.3) ch2 = MCP3208(channel = 2, differential = False, max_voltage = 3.3) ch3 = MCP3208(channel = 3, differential = False, max_voltage = 3.3) thelcd = I2C_LCD_driver.lcd() while True: # Define Celcius/Farenheit Celcius = round(ch3.voltage * 100, 1) Ferenheit = round((Celcius * 9/5) + 32, 1) #commented out code(code to test the MCP3208) #print("The temperature is %6.2f C." % Celcius) #print("The temperature is %6.2f F." % Ferenheit) thelcd.lcd_display_string("Ch0 Raw:" +str(ch0.raw_value)+str("V")) thelcd.lcd_display_string(str(Celcius)+"C | "+str(Ferenheit)+"F",2,0) time.sleep(5)
from gpiozero import MCP3208 import time import csv csvfile = 'MCP3208.csv' adc = [0, 1, 2, 3, 4, 5, 6, 7] vref = 3.3 while True: with MCP3208(channel=0) as reading: adc0 = reading.value * vref adc0temp = ('{:.1f}'.format(adc0)) with MCP3208(channel=1) as reading: adc1 = reading.value * vref adc1temp = ('{:.1f}'.format(adc1)) with MCP3208(channel=2) as reading: adc2 = reading.value * vref adc2light = ('{:.1f}'.format(adc2)) with MCP3208(channel=3) as reading: adc3 = reading.value * vref adc3light = ('{:.1f}'.format(adc3)) with MCP3208(channel=4) as reading: adc4 = reading.value * vref adc4levelt = ('{:.1f}'.format(adc4)) with MCP3208(channel=5) as reading:
For WaferSat UROP Thermal Test Circuit with PCBSat Rev1 Python file for using Raspberry Pi as a data acquisition unit for measuring temperatures using RTDs. Note that this file assumes that all 8 channels are functioning and connected to RTDs. If not all channels are being used, specify how many channels are being used in the plot_temp function and make sure your circuit is only using the first i channels on the MCP3208, where i is the number of desired channels. """ from gpiozero import MCP3208 from time import sleep import time import csv import RPi.GPIO as GPIO #led = LED(21) #initializes status LED connected to GPIO21 adc = MCP3208() RTD0 = MCP3208( channel=0, port=0, device=0 ) #ADC input 0 settings using MCP3208 hardware interface (MOSI: GPIO10, MISO: GPIO9, CLK: GPIO11, CS0: GPIO8) RTD1 = MCP3208(channel=1, port=0, device=0) RTD2 = MCP3208(channel=2, port=0, device=0) RTD3 = MCP3208(channel=3, port=0, device=0) RTD4 = MCP3208(channel=4, port=0, device=0) RTD5 = MCP3208(channel=5, port=0, device=0) RTD6 = MCP3208(channel=6, port=0, device=0) RTD7 = MCP3208(channel=7, port=0, device=0) # RTD_list = [RTD0, RTD1, RTD2, RTD3, RTD4, RTD5, RTD6, RTD7] #led.on()
#! /usr/bin/python3 # -*- coding: utf-8 -*- # # gpiozero01.py # # Feb/06/2017 # # -------------------------------------------------------------------- from gpiozero import MCP3208 from time import sleep # -------------------------------------------------------------------- pot = MCP3208(channel=0) while True: ratio = pot.value inum = int(4096 * ratio) print(ratio, inum) sleep(1) # --------------------------------------------------------------------
from gpiozero import MCP3208 from time import sleep mcp = MCP3208(channel=0) print( '| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |' .format(*range(8))) print('-' * 57) # Main program loop. while True: # Read all the ADC channel values in a list. values = [0] * 8 for i in range(8): # The read_adc function will get the value of the specified channel (0-7). values[i] = mcp.read_adc(i) # Print the ADC values. print( '| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |' .format(*values)) # Pause for one second. sleep(1)
# Created Daichi Hayashi 2017/12/18 # From Yonago Institute of Technology # Copyright ©️ 2017 Daichi Hayashi. All rights reserved. # move 3 fingers following to proximity sensors # this might be main program # this program is made for an experiment # ========== import ========== import pigpio from gpiozero import MCP3208 import time from datetime import datetime adc0 = MCP3208(channel=0) Vref = 5.05 # reference voltage of MCP3208 pwm = pigpio.pi() # ========== constants ========== fing0Pin = 15 # Threshold distance to stop motor[mm] stopDst = 20.0 # start distance of control[mm] maxDst = 100.0 # speed of servoMotor[deg/sec] maxSpeed = 20.0 # lowest speed minSpeed = 2.0
from gpiozero import MCP3208, LED from time import sleep deg = chr(176) + 'C' tmp = MCP3208(channel=0, device=0) red = LED(19) while True: temperature = (tmp.value * 3.3 - 0.5) * 100 print(temperature) #temperature=tmp.value/0.02793 #if temperature >24: # red.on() #else: # red.off() #print('{:.1f}'.format(temperature), deg, 10 * ' ') sleep(0.1) ############ # This would show you the voltage from the TMP36 # print(tmp.value * 3.3, " V") # Voltage # This would show you the temperature to lots of dp # print((tmp.value * 3.3 - 0.5)*100, " C ")
from gpiozero import MCP3208, RGBLED from time import sleep led = RGBLED(red=19,green=21,blue=26) adc = MCP3208(channel=1) led.color = (0,178/255,1) while True: voltage = 3.3*adc.value print(voltage) sleep(0.5)