def setup(): #setup light to voltage sensor apin = 34 # define analog pin nr (pin A2) adc = machine.ADC( machine.Pin(apin)) # define the pin as an analog-to-digital converter adc.atten(machine.ADC.ATTN_11DB) # set the atten..? adc.width(machine.ADC.WIDTH_12BIT) # set the width of the available bits #setup LED apin2 = 25 #define the LED as analog output (pin A1) dac = machine.DAC(machine.Pin(apin2)) #define as adc.atten(machine.ADC.ATTN_11DB) # set the atten..? adc.width(machine.ADC.WIDTH_12BIT) # set the width of the available bits #setup embedded LED ledPinNo = 13 #this pin nr. is fixed (pin 13) LED = machine.Pin(ledPinNo2, machine.Pin.OUT) #setup button 1 buttomPinNo = 27 #insert pin number for button (pin 27) button = machine.Pin(buttomPinNo, machine.Pin.IN) # setup button 2 buttomPinNo2 = 12 # insert pin number for button (pin 12) button2 = machine.Pin(buttomPinNo2, machine.Pin.IN) return (adc, dac, LED, button, button2)
def angle_start(obj): lcd.image(0, 0, '/flash/img/3-9.jpg', type=lcd.JPG) np = machine.Neopixel(machine.Pin(15), 10) np.brightness(0) np.set(1, lcd.WHITE, num=10) obj['np'] = np obj['angle'] = units.ANGLE(units.PORTB) obj['prev'] = 0 dac = machine.DAC(machine.Pin(25)) dac.write(0) lcd.font(lcd.FONT_DejaVu24)
def micro_start(obj): lcd.image(0, 0, '/flash/img/3-2.jpg', type=lcd.JPG) dac = machine.DAC(25) dac.write(0) adc = machine.ADC(34) adc.atten(adc.ATTN_11DB) obj['adc'] = adc buffer = [] for i in range(0, 55): buffer.append(0) obj['buf'] = buffer
def angle_start(obj): lcd.image(0, 0, '/flash/img/3-9.jpg', type=lcd.JPG) # np = machine.Neopixel(machine.Pin(15), 10) ledbar.setBrightness(0) ledbar.setColorAll(lcd.WHITE) # obj['np'] = np obj['angle'] = unit.get(unit.ANGLE, unit.PORTB) obj['prev'] = 0 dac = machine.DAC(machine.Pin(25)) dac.write(0) lcd.font(lcd.FONT_DejaVu24)
def play_sound(times): def chime(): for t in range(10000): dac_a.write( int(t * ((15 & t >> 11) % 12) & 55 - (t >> 5 | t >> 12) | t * (t >> 10) * 32) & 255) dac_pin = machine.Pin(25) dac_a = machine.DAC(dac_pin) for _ in range(times): chime() time.sleep(0.5)
def run_loop(): dac = machine.DAC('P22') # create a DAC object adc = machine.ADC() # create an ADC object apin = adc.channel(pin='P16') global counter while True: time.sleep(1) val = apin() val= val/400 val= val/10 dac.write(val) print(str(counter))
def microphone_enter(): print('microphone_enter') global mic_adc, buffer try: mic_adc = machine.ADC(34) mic_adc.atten(mic_adc.ATTN_11DB) dac = machine.DAC(machine.Pin(25)) dac.write(0) except: pass buffer = [] for i in range(0, 55): buffer.append(0)
def __init__(self, apin, dpin): self.sharpVoPin = apin self.sharpLEDPin = dpin self.dac = machine.DAC( self.sharpLEDPin) # create a DAC object on pin P20 G7 self.adc = machine.ADC() # create an ADC object self.apin = self.adc.channel( pin=self.sharpVoPin, attn=ADC.ATTN_11DB) # create an analog pin on P21 G8 VoRawTotal = 0 self.Vo = 0 self.dV = 0 #Set the typical output voltage in Volts when there is zero dust. self.Voc = 100 #mV
def __init__(self): print("Setting properties.") # store and initialize I/O analogue pins self.pin_V_in = par["pin_V_in"] self.V_in = machine.ADC(self.pin_V_in) self.pin_V_out = par["pin_V_out"] self.V_out = machine.DAC(self.pin_V_out) self.pin_I_in = par["pin_I_in"] self.I_in = machine.ADC(self.pin_I_in) self.pin_I_out = par["pin_I_out"] self.I_out = machine.DAC(self.pin_I_out) # reference voltage # pins self.vref = par["vref"] self.vref_1_3 = par["vref_1/3"] self.vref_2_3 = par["vref_2/3"] self.vref_dac_cal = par["vref_dac_cal"] # voltage set values self.vref_1_3_set = par["vref_1/3_setVoltage"] self.vref_2_3_set = par["vref_2/3_setVoltage"] # digital set values self.dref_1_3_set = par["dref_1_3_setDigital"] self.dref_2_3_set = par["dref_2_3_setDigital"] # default dac self.adc_slope = par["adc_slope"] self.adc_intercept = par["adc_intercept"] # default dac self.dac_max_V_design = par["dac_max_V_design"] self.dac_max_dig = par["dac_max_dig"] # modbus related parameters self.slave_address = par["modbus"]["address"] self.tx = par["modbus"]["tx"] self.rx = par["modbus"]["rx"] self.baudrate = par["modbus"]["baud"] self.databits = par["modbus"]["bits"] self.stopbits = par["modbus"]["stop"] self.parity = par["modbus"]["parity"] self.uartTimeout = par["modbus"]["timeout"] self.control_pin = par["modbus"]["control_pin"] self.available_regs = par["modbus"]["available_regs"] # initialize modbus device over UART self.modbus = uModBusSerial(slave_address=self.slave_address, baudrate=self.baudrate, data_bits=self.databits, stop_bits=self.stopbits, parity=self.parity, timeout=self.uartTimeout, ctrl_pin=self.control_pin, pins=[self.tx, self.rx]) # conversion parameters # 0..10 V voltage divider self.V_in_R1 = par["0..10V_in"]["R1"] self.V_in_R2 = par["0..10V_in"]["R2"] # 0..10 V OP amplifier self.V_out_R1 = par["0..10V_out"]["R1"] self.V_out_R2 = par["0..10V_out"]["R2"] # 0..20 V current loop input self.I_in_R1 = par["0..20mA_in"]["R1"] # 0..20 V current loop output self.I_out_R1 = par["0..20mA_out"]["R1"] # set reference voltage and calibrate ADC as well as DAC self.set_ESP_referenceVoltage() self.calibrate()
import pycom import ubi #ubidots ## -- INPUT OBJECTS -- ## adc = machine.ADC() # ADC object for temp sensor tempPin = adc.channel( pin='P16') # Read voltage from pin 16. Temperature sensor is here. # Make 'P19' an input with the pull-down enabled smokePin = Pin('P9', mode=Pin.IN, pull=Pin.PULL_DOWN) # Set 'P20' as an input pirPin = Pin('P20', mode=Pin.IN, pull=Pin.PULL_DOWN) ## -- SOUND -- ## soundPin = machine.DAC( 'P21') # Use either P21 or P22. They are the only DAC outputs, which can # output specific tones for the piezo element. def soundAlarm(): soundPin.tone( 2000, 2 ) # Here you can play around with different tones, delays, etc. I found time.sleep( 0.1) # this to sound the most like an alarm, but you'll probably find soundPin.tone(3000, 1) # something better :) time.sleep(0.2) soundPin.tone(4000, 0) # syntax for tone(): time.sleep( 0.3) # tone(frequency, volume lowering (0-3, higher is lower volume))
import machine import math import time import connect import ntptime import machine connect.do_connect(verbose=True) ntptime.settime() rtc = machine.RTC() speakerpin = machine.Pin(25) dac = machine.DAC(speakerpin) def dacspeedtest(): nomadcfreq = 8000 duration = 1 nomperiod_us = 1000000 // nomadcfreq nomaudiofreq = 1500 bytes = bytearray([ int(128 + 120 * math.sin(math.pi * i * nomaudiofreq / nomadcfreq)) for i in range(int(nomadcfreq * duration)) ]) for i in range(10): for b in bytes: dac.write(b)
""" TOCS example usage """ import machine dac = machine.DAC('P22') # create a DAC object adc = machine.ADC() # create an ADC object apin = adc.channel(pin='P13', attn=machine.ADC.ATTN_0DB) # create an analog pin on P13 while True: print("DAC input : Please type a float value between 0 and 0.5 : ", end="") input_value = float(input()) if input_value >= 0.0 and input_value <= 0.5: dac.write(input_value) print("ADC read value is %s mv" % apin.voltage())
def __init__(self,port, bits=8, *, buffering=None): p=machine.Pin(port) self.d=machine.DAC(p)
import math import machine dac1 = machine.Pin(25) speaker = machine.DAC(dac1) C4 = 261.626 D4 = 293.665 E4 = 329.628 F4 = 349.228 G4 = 391.995 A4 = 440 B4 = 493.883 C5 = 523.351 max_volume = 127 baselength = 3000 def playNote(note, length=1, volume=max_volume): notelength = baselength * length for t in range(notelength): speaker.write(128 + int(volume * math.sin(2 * math.pi * note * t / baselength))) def playSong(song): for note in song: playNote(*note)
from m5stack import lcd import machine lcd.print('dac init start \n') dac = machine.DAC(machine.Pin(26)) dac.write(128) lcd.print('dac init done ... \n')
def __init__(self, pin, bits=12, write=255): self.bits = bits self.led = machine.DAC(machine.Pin(pin), bits=bits) self.led.write(write)
import os import machine import random import time # os.listdir() p = machine.Pin(25) a = machine.DAC(p) t = 0 q = machine.Pin(32) b = machine.ADC(q) while True: reading = b.read() if reading < 400: reading = 0 else: reading -= 400 print(reading) a.write(reading % 200) while True: reading = b.read() reading -= 40 reading = max(0,reading) print(reading) a.write(reading % 255) time.sleep(0.002)
import ads1x15 import hp4067 import machine import math import json import sys from time import sleep # DAC Setup dac = machine.DAC(machine.Pin(25)) # Multiplexer Setup R_mux = [1000e3, 470e3, 220e3, 100e3, 10e3, 4.7e3] pin_en = machine.Pin(25, machine.Pin.OUT) pin_s0 = machine.Pin(19, machine.Pin.OUT) pin_s1 = machine.Pin(18, machine.Pin.OUT) pin_s2 = machine.Pin(5, machine.Pin.OUT) pin_s3 = machine.Pin(17, machine.Pin.OUT) muxer = hp4067.HP4067(pin_en, pin_s0, pin_s1, pin_s2, pin_s3) muxer.enable() muxer.set_channel(0) # ADC Setup ads_address = 72 adc_gain = 0 adc_rate = 6 pin_scl = machine.Pin(22) pin_sda = machine.Pin(23)
import machine import math # Set up the two DAC pins d = machine.DAC(machine.Pin(25, machine.Pin.OUT), bits=12) d1 = machine.DAC(machine.Pin(26, machine.Pin.OUT), bits=12) # To write a constant value, use, e.g.: # d.write(200) # Create buffers for the two modulated sine-waves buf = bytearray(5000) buf1 = bytearray(5000) # Assign modulated sine wave, with modulating signal 50x slower than carrier frequency for i in range(len(buf)): buf[i] = 128 + int(127 * math.sin(2 * math.pi * 50 * i / len(buf)) * (0.5 + 0.5 * math.sin(2 * math.pi * i / len(buf)))) buf1[i] = 128 + int(127 * math.cos(2 * math.pi * 50 * i / len(buf)) * (0.5 + 0.5 * math.sin(2 * math.pi * i / len(buf)))) # Sequentially output the values in the two buffers. Results in a carrier frequency of about # 100 Hz. for k in range(10000): for i in range(len(buf)): d.write(buf[i]) d1.write(buf1[i]) # The below syntax will alternatively write the byte array to the DAC pin much more quickly (up to a rate of 500 kHz) but # seems unable to output on both pins simultaneously at higher frequencies and unable to find a way # to sync the two outputs in order to be able to use the difference between them.
import machine import utime import _thread import ssd1306 i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5)) oled = ssd1306.SSD1306_I2C(128, 64, i2c) dac = machine.DAC(machine.Pin(25, machine.Pin.OUT), bits=12) adc = machine.ADC(machine.Pin(36, machine.Pin.IN)) oled.fill(0) oled.text('Aguardando...', 1, 1) oled.show() # t = _thread.start_new_thread(input_thread, ()) i = 0 while True: try: value_in = str(adc.read()) + '\n' value_out = input(value_in) dac.write(int(value_out)) # print(value_out) #utime.sleep_ms(10) oled.fill(0) oled.text(str(value_out), 1, 1) oled.text(str(value_in), 1, 20) oled.text(str(i), 1, 40) oled.show() i += 1 except KeyboardInterrupt:
def ttimerSampler(): global ledIsGreen # ADC0 connect to a piezoelectric "x'mas card" speaker in parallel to a 1M Ohm resistor. Taking small spike as knock input if (adc0.read()) > 20: handleTargetHit() pass @timerSch.event('timerLedOff') def ttimerLedOff(): global ledIsGreen rgb.setBrightness(0) pass adc0 = machine.ADC(33) adc0.width(machine.ADC.WIDTH_12BIT) adc0.atten(machine.ADC.ATTN_11DB) dac0 = machine.DAC(25) # LED boot diagnostic to show system is booting properly rgb.setBrightness(10) rgb.setColorAll(0xff0000) wait(1) rgb.setColorAll(0x33ff33) wait(1) rgb.setColorAll(0x3333ff) wait(1) rgb.setBrightness(0) timerSch.run('timerSampler', 10, 0x00) while True: wait_ms(2)
################## End setup (EDIT END HERE) import socket, os, pycom, crypto, socket, gc, machine, utime, ubinascii, ustruct from machine import SD, WDT, deepsleep, Pin, Timer from network import LoRa, WLAN from crypto import AES from uModBus.serial import Serial from uModBus.tcp import TCP iv = crypto.getrandbits(128) # hardware generated random IV (never reuse it) gc.enable() wdt = WDT(timeout=4000000) adc = machine.ADC() dac = machine.DAC('P21') # Turn off WiFi to save power w = WLAN() #w.deinit() chrono = Timer.Chrono() chrono.start() volt = adc.channel(pin='P20') dac.write(1) pycom.heartbeat(False) pycom.rgbled(0x007f00) # Turn on Green LED
def _led_handler(self, alarm): dac = machine.DAC('P22') # create a DAC object self.led *= -1 dac.write(self.led)
import machine, time p = machine.Pin(25, machine.Pin.OUT) dac = machine.DAC(p) with open('tron_bit_yes_8bit_w.wav', 'rb') as f: buf = f.read() for i in buf: dac.write(i) time.sleep(.001) dac.write(0)
async def init(self): port = int(self._config.get('port')) self._dac = machine.DAC( machine.Pin(port, mode=machine.Pin.OUT) )