def experiment(starting_value, button_pin, sensor_pin, led_pin, blue, red, green): button = machine.Pin(button_pin, machine.Pin.IN) #define button pin adc33 = ADC(machine.Pin(sensor_pin)) #define sensor pin adc33.atten(ADC.ATTN_6DB) #define adc dac26 = DAC(machine.Pin(led_pin)) #define DAC dac26.write(starting_value) #adjust the intensity of the LED data = [] i = 0 counter = 0 while not count_time(button_pin): print("here!!") if button.value() == 1: color("green", blue, red, green) #if the experiment runs neopixel gives green color if counter < 5: create_data_file(starting_value, button_pin, sensor_pin, led_pin, blue, red, green, counter) #create new data file counter += 1 color("purple", blue, red, green) color("white", blue, red, green) time.sleep(5) color("no", blue, red, green) #Take the of the measurments every 1,2 seconds sum = 0 for i in range(120): if button.value() == 1: break sum = sum + adc33.read() time.sleep(0.01) average = sum / 12000
class ODSensor: # frequency and pins may need to be changed def __init__(self,LEDPin, sensorPin): #25,39. 26,36. self.DAC_MAX = 255 self.DAC_Vmax = 3.15 self.DAC_Vmin = 0.09 self.led = DAC(Pin(25), bits=8) self.sensor = ADC(Pin(39)) #Green #self.sensor.atten(ADC.ATTN_11DB) self.sensor.width(ADC.WIDTH_10BIT) # return a single measurement def measure(self, intensity): self.led.write(intensity) data = [] #get 120 measurements for j in range(120): data.append(self.sensor.read()) data.sort() #sort data increasing sum_middle = sum(data[30:90]) #find sum of middle numbers avg = sum_middle / 60 #find average of middle numbers return avg
def __init__(self): self.coils = [pins.motor_1, pins.motor_2, pins.motor_3, pins.motor_4] self.vref_12 = DAC(pins.motor_vref_a) self.vref_34 = DAC(pins.motor_vref_b) self.sequence = [[1, 0, 0, 0], [1, 1, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1]]
def sound(frequency_Hz, duration): dac1 = DAC(Pin(25)) dac2 = DAC(Pin(26)) # create a buffer containing a sine-wave buf = bytearray(256) for i in range(len(buf)): buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf))) dacOut = dac2.write #spped optimization l = len(buf) #spped optimization lm1 = (l - 1) * l fs = 91839 numsamples = fs * duration factor = int(frequency_Hz * lm1 / fs) index = 0 for n in range(numsamples): dacOut(buf[index >> 8]) index = index + factor if (index >= lm1): index = index = 0
def confService(self): self.powerPin = Pin('P8', mode=Pin.OUT) self.adc = ADC() self.adc.vref(1058) self.vBiasDAC = DAC('P22') self.vBiasDAC.write(0.135) # approximately 0.5 V self.Battery = self.adc.channel(pin='P14', attn = ADC.ATTN_11DB)
class Speak: def __init__(self): # init dac self.dac = DAC(Pin(25)) # init params self.__volume = 8 def set_volume(self, volume): if (volume <= 10) and (volume >= 0): self.__volume = 11 - volume else: print('the volume must be in 0 to 10') def play_music(self, filename, rate): delay_interval = int(1000000 / rate) last_data = 0 f = open(filename, 'r') if self.__volume != 11: while True: data = f.read(4) if data == '': for i in range(int(last_data / self.__volume), 0, -1): self.dac.write(i) time.sleep_ms(2) f.close() return else: data = int(data.strip()) self.dac.write(int(data / self.__volume)) time.sleep_us(delay_interval) last_data = data
class BatteryService(object): def __init__(self): self.powerPin = 0 self.adc = 0 self.Battery = 0 self.vBiasDAC = 0 def confService(self): self.powerPin = Pin('P8', mode=Pin.OUT) self.adc = ADC() self.adc.vref(1058) self.vBiasDAC = DAC('P22') self.vBiasDAC.write(0.135) # approximately 0.5 V self.Battery = self.adc.channel(pin='P14', attn = ADC.ATTN_11DB) def getData(self): self.powerPin(1) time.sleep(0.002) batt = self.Battery.voltage() collectMemory() self.powerPin(0) return batt def connect(self): self.confService()
def create_data_file(starting_value, button_pin, sensor_pin, led_pin, blue, red, green, counter): button = machine.Pin(button_pin, machine.Pin.IN) #define button pin adc33 = ADC(machine.Pin(sensor_pin)) #define sensor pin adc33.atten(ADC.ATTN_6DB) #define adc dac26 = DAC(machine.Pin(led_pin)) #define DAC dac26.write(starting_value) #adjust the intensity of the LED data = [] i = 0 f = open('data' + str(counter), 'w') while True: time.sleep(1) #Take the average of the measurments every 1,2 seconds sum = 0 for i in range(12000): if button.value() == 1: break sum = sum + adc33.read() time.sleep(0.01) average = sum / 12000 data.append(average) f.write("%s\n" % average) print("\n") print(average) if button.value() == 1: #button is pressed again color("blue", blue, red, green) time.sleep(1) break i = i + 1 f.close()
class A_Pin(D_Pin): def __init__(self, pin): #self.pin = pin self.dac = None self.adc = None if pin in [25, 26]: self.dac = DAC(Pin(pin)) if pin in [32, 33]: self.adc = ADC(Pin(pin)) self.adc.atten(ADC.ATTN_11DB) self.adc.width(ADC.WIDTH_10BIT) super().__init__(pin) def write_analog(self, value): if self.pin not in [25, 26]: # print("This pin feature is not supported") super().write_analog(value) else: self.dac.write(value) def read_analog(self): if self.adc != None: return self.adc.read() else: print('This Pin does not support ADC') return None
def udefault(): #tipo1=1 #tipo2=2 maxx = 1 puntos = 90 presicion = 4 amplitud = 16 f = 5 phi = 0 print("Default. Procesando...") #print("holi") func1 = ondas.onda(maxx, puntos, presicion, amplitud, f, phi, 2) func2 = ondas.onda(maxx, puntos, presicion, amplitud, f, phi, 1) for i in range(len(func1)): print("{", func1[i], "}", "{", func2[i], "}") func1[i] = 128 + round(func1[i]) func2[i] = 128 + round(func2[i]) gc.collect() dac1 = DAC(Pin(25, Pin.OUT)) dac2 = DAC(Pin(26, Pin.OUT)) try: plotdac.pltdac(func1, func2, dac1, dac2) except Exception as e: print(e)
def __init__(self,LEDPin, sensorPin): #25,39. 26,36. self.DAC_MAX = 255 self.DAC_Vmax = 3.15 self.DAC_Vmin = 0.09 self.led = DAC(Pin(25), bits=8) self.sensor = ADC(Pin(39)) #Green #self.sensor.atten(ADC.ATTN_11DB) self.sensor.width(ADC.WIDTH_10BIT)
def __init__(self, pin): #self.pin = pin self.dac = None self.adc = None if pin in [25, 26]: self.dac = DAC(Pin(pin)) if pin in [32, 33]: self.adc = ADC(Pin(pin)) self.adc.atten(ADC.ATTN_11DB) self.adc.width(ADC.WIDTH_10BIT) super().__init__(pin)
def signal_generator(pin=25, buffer_size=100, freq=.1): # create a buffer containing a sine-wave buf = bytearray(buffer_size) for i in range(len(buf)): buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf))) # output the sine-wave at 400Hz p = Pin(pin, Pin.OUT) dac = DAC(p) while True: for i in buf: dac.write(i) time.sleep(freq)
def play(filename, pin): try: p = Pin(pin, Pin.OUT) dac = DAC(p) except Exception as e: return str(e) f = wave.open(filename, 'r') total_frames = f.getnframes() framerate = f.getframerate() for position in range(0, total_frames, framerate): f.setpos(position) # dac.write_timed(f.readframes(framerate), framerate) dac.write(f.readframes(framerate)) sleep_ms(1000)
def main(): pkt_tx_queue = [] def audio_frame_ready(data): pkt_tx_queue.append(data) lora_ctl = lora.LoRaController() spk = speaker.Speaker(DAC('P22')) adc = ADC() apin = adc.channel(pin='P13', attn=ADC.ATTN_11DB) uphone = microphone.Microphone(apin, audio_frame_ready) tlk_btn = talk_button.TalkButton(uphone) print('Started ...') flash(0x007f00) # green while True: Timer.sleep_us(1000) # Handle the RX packets # TODO: refactor to use callback mechanism. while True: data = lora_ctl.recv() if data: spk.enque(data) else: break # Handle the TX queue # TODO: refactor to use Python synchronized Queue. while pkt_tx_queue: data = pkt_tx_queue.pop(0) print('.') lora_ctl.send(data)
def __init__(self, host='0.0.0.0', port=5000, backlog=5, timeout=20000): self.host = host self.port = port self.backlog = backlog self.timeout = timeout self.pulse_data = [0]*500 self.pulse_height = 0 self.pulse_T = 0 self.pulse_form = 0 self.pulse_height_old = self.pulse_height self.pulse_T_old = self.pulse_T self.pulse_form_old = self.pulse_form self.generating = False self.led = Pin(LED_PIN,Pin.OUT) self.dac = DAC(Pin(DAC_PIN))
def test(): buf = bytearray(100) dac = DAC(Pin(25, Pin.OUT)) for i in range(len(buf)): buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf))) #print(1 "{",buf[i],"}") #dac.write(buf[i]) test_count=0 flag= False while flag==False: for i in range(len(buf)): print(1, "{",buf[i],"}") dac.write(buf[i]) time.sleep(0.1) test_count=test_count+1 if test_count==9000: flag=True print("end 1")
def experiment(starting_value, button_pin, sensor_pin, led_pin): button = machine.Pin(button_pin, machine.Pin.IN) #define button pin adc33 = ADC(machine.Pin(sensor_pin)) #define sensor pin adc33.atten(ADC.ATTN_11DB) #define adc dac26 = DAC(machine.Pin(led_pin)) #define DAC dac26.write(starting_value) #adjust the intensity of the LED data = [] i = 0 while True: if button.value() == 1: f = open('data', 'w') while True: time.sleep(0.1) data.append(adc33.read()) f.write("%s\n" % adc33.read()) # print(data[i - 1]) if button.value() == 1: time.sleep(1) break i = i + 1 f.close()
def confService(self, atributes): self.powerPin = Pin('P8', mode=Pin.OUT) self.adc = ADC() self.adc.vref(1058) self.vBiasDAC = DAC('P22') self.vBiasDAC.write(0.135) # approximately 0.5 V self.panel = self.adc.channel(pin='P13', attn=ADC.ATTN_11DB) self.errorLogService = atributes['errorLogService'] self.lock = atributes['lock'] if ('mode' in atributes) and ('samplingFrequency' in atributes): if not str(atributes['samplingFrequency']).isdigit() or atributes[ 'samplingFrequency'] < 0: #Comprobar si es un numero (isdigit) y si es negativo self.errorLogService.regError( self.serviceID, -9) #Incorrect AtributeValue Error else: self.samplingFrequency = atributes['samplingFrequency'] if not str(atributes['mode']).isdigit() or atributes[ 'mode'] < 0: #Comprobar si es un numero (isdigit) y si es negativo self.errorLogService.regError( self.serviceID, -9) #Incorrect AtributeValue Error else: self.mode = atributes['mode'] else: self.errorLogService.regError(self.serviceID, -2) #ConfFile Error
def audio_loopback(): def handle_audio(data): spk.enque(data) int_mode = False spk = speaker.Speaker(DAC('P22'), int_mode=int_mode, debug=False) adc = ADC() apin = adc.channel(pin='P13', attn=ADC.ATTN_11DB) uphone = microphone.Microphone(apin, handle_audio, int_mode=int_mode) tlk_btn = talk_button.TalkButton(uphone) print('Audio playpack ...') flash(0x000010) # Dark blue while True: if int_mode: Timer.sleep_us(1000000) else: uphone.loop() spk.loop()
class Stepper: def __init__(self): self.coils = [pins.motor_1, pins.motor_2, pins.motor_3, pins.motor_4] self.vref_12 = DAC(pins.motor_vref_a) self.vref_34 = DAC(pins.motor_vref_b) self.sequence = [[1, 0, 0, 0], [1, 1, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1]] def activate(self, index): values = self.sequence[index] for i in range(4): self.coils[i].on() if values[i] else self.coils[i].off() def step(self): self.vref_12.write(30) self.vref_34.write(30) for i in range(len(self.sequence)): self.activate(i) sleep_us(10000) self.vref_12.write(0) self.vref_34.write(0)
def dac_write(self, val): id = int(str(self)[4:-1]) #unsafe! self = DAC(Pin(id)).write(val)
from machine import Pin, DAC from time import sleep dac = DAC(Pin(26)) print("Running a triangular wave form with a frequency of ~ 1 Hz on pin 26") while True: dac.write(0) sleep(2) dac.write(255) sleep(2)
DAC_MAX = 255 DAC_Vmax = 3.15 DAC_Vmin = 0.09 # Not used! DAC_QUANTUM = DAC_Vmax / DAC_MAX look_up_ADC = [] # Initialize ADC adc = ADC(Pin(ADC_PIN_NO)) adc.atten(ADC.ATTN_11DB) adc.width(ADC.WIDTH_10BIT) # was 10 # Initialize DAC dac = DAC(Pin(DAC_PIN_NO), bits=8) # Measure adc_read = [] for i in range(0, DAC_MAX+1): print('Samples acquired: ' + str(i) + '/' + str(DAC_MAX)) dac.write(i) utime.sleep_ms(ADC_DELAY) raw_read = [] for i in range(1, NUM_SAMPLES+1): raw_read.append(adc.read()) utime.sleep_ms(DAC_DELAY) adc_read.append(round(sum(raw_read)/NUM_SAMPLES)) # Print result #print(adc_read)
from machine import DAC, Pin dac = DAC(Pin(25)) dac.write(128)
# f = 329Hz # fs = 32.9kHz import math from machine import DAC from machine import Pin dac1 = DAC(Pin(25)) dac2 = DAC(Pin(26)) # create a buffer containing a sine-wave buf = bytearray(100) for i in range(len(buf)): buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf))) while 1: for i in range(len(buf)): dac2.write(buf[i])
from machine import DAC, Pin da1 = DAC(Pin(26, Pin.OUT), bits=8) value = 2 da1.write(int(value * 255 / 3.3))
from machine import DAC, Pin import utime da = DAC(Pin(26)) da.write(0)
''' Send the value 0..256 to the DAC and read the analogue value back on the ADC Copyright (c) U. Raich 2020 This program is part of the course on IoT at the University of Cape Coast, Ghana ''' from machine import Pin, ADC, DAC from time import sleep_ms adc = ADC(Pin(36)) # create ADC object on ADC pin adc.atten(ADC.ATTN_11DB) dac = DAC(Pin(26)) file = open("linearity.dat", "w") print( "Send values from 0 ..255 to DAC and read the signal level back on the ADC" ) print("The ADC values are saved in the file 'linearity.dat'") for i in range(256): # send the value to the DAC and wait for 50 ms to stabilize # then read back the signal level on the DAC dac.write(i) sleep_ms(50) file.write("{:d}\n".format(adc.read())) for i in range(255): dac.write(254 - i) sleep_ms(50) file.write("{:d}\n".format(adc.read())) file.close()
def pltdac(func1, func2, dac1, dac2): test_count = 0 flag = False print("ok dac") while flag == False: for i in range(len(func1)): #print("{",func1[i],"}", "->","{",func2[i],"}") dac1.write(func1[i]) dac2.write(func2[i]) time.sleep(0.1) test_count = test_count + 1 if test_count == 9000: flag = True if __name__ == "__main__": print("Modulo two dac") dac1 = DAC(Pin(25, Pin.OUT)) dac2 = DAC(Pin(26, Pin.OUT)) func1 = bytearray(100) func2 = bytearray(100) for i in range(len(func1)): func1[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(func1))) func2[i] = 128 + int(127 * math.cos(2 * math.pi * i / len(func1))) print("ploteando") pltdac(func1, func2, dac1, dac2) else: print("Modulo two dac importado")