示例#1
0
from machine import SoftI2C, UART, Pin
import ssd1306  #引入ssd1306模块,ssd1306.py
import re

i2c = SoftI2C(sda=Pin(15), scl=Pin(16))
#设置I2C的SDA与SCL所在的引脚

oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)
#创建一个ssd1306.SSD1306_I2C类的对象,初始化并设置屏幕像素,i2c引脚,i2c地址

uart1 = UART(1, tx=17, rx=18)
#选择UART接口,指定TX与RX使用的引脚

uart1.init(115200, bits=8, parity=None, stop=1)
#初始化,设置波特率,设置字符位数,设置奇偶校验,设置停止位

while True:
    uart1.any()  #检索是否收到信息,否则返回0,是则返回信息
    if not uart1.any() == 0:
        oled.fill(0)
        read = uart1.read()
        print(read)
        only_text = re.sub('\r' + '\n', ' ', read)
        oled.text(only_text, 0, 32)
        #设置在屏幕中将要显示的文字内容,设置起始像素点坐标
        oled.show()
        #将内容输出到oled屏幕上
示例#2
0
文件: main.py 项目: georgehart/BNO055
# ----------------------------------------------

from machine import SoftI2C, Pin
from machine import UART
import time
import bno055

# ----- Initialisation Serial Port -----
'''
uart = UART(1, 38400)                         # init with given baudrate
uart.init(38400, bits=8, parity=None, stop=1) # init with given parameters
t = uart.write(bytes('Georges Hart',"UTF-8"))
'''

# ----- I2C initialisation -----
i2c = SoftI2C(Pin(22), Pin(23), timeout=1000)

# ----- BNO055 setup
s = bno055.BNO055(i2c)
s.mode = bno055.GYRONLY_MODE  # only Gyrocope is active (see bno055.py)
s.reset()
s.init()

# print('Temperature :{} degrees °C'.format(s.temperature()))

file = open("log.gh", "w")
file.write("Log file")
file.close()

while True:
示例#3
0
def create_i2c(pin_scl, pin_sda):
    return SoftI2C(pin_scl, pin_sda)
示例#4
0
# Complete project details at https://RandomNerdTutorials.com

from machine import Pin, SoftI2C
import ssd1306
from time import sleep

# Start I2C Communication SCL = 4 and SDA = 5 on Wemos Lolin32 ESP32 with built-in SSD1306 OLED
i2c = SoftI2C(scl=Pin(4), sda=Pin(5))

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

oled.text('Hello, World 1!', 0, 0)
oled.text('Hello, World 2!', 0, 10)
oled.text('Hello, World 3!', 0, 20)

oled.show()
示例#5
0
    print("Configuring OLED.")
    
    # configure OLED display
    oled_width = 128
    oled_height = 64
    # OLED reset pin
    i2c_rst = Pin(16, Pin.OUT)
    # Initialize the OLED display
    i2c_rst.value(0)
    time.sleep(0.010)
    i2c_rst.value(1) # must be held high after initialization
    # Setup the I2C lines
    i2c_scl = Pin(15, Pin.OUT, Pin.PULL_UP)
    i2c_sda = Pin(4, Pin.OUT, Pin.PULL_UP)
    # Create the bus object
    i2c = SoftI2C(scl=i2c_scl, sda=i2c_sda)
    # Create the display object
    oled = SSD1306_I2C(oled_width, oled_height, i2c)
    oled.fill(0)
    oled.text('Samim SmartHome', 0, 10)
    oled.text('LoRaMQTTDev '+str(DEV_VER), 0, 20)
    oled.show()
    print("OLED configured")

if module_config['module_type']=='ESP32':
    print("Configuring ESP32.")
    device_spi = SoftSPI(baudrate = 10000000, 
            polarity = 0, phase = 0, bits = 8, firstbit = SoftSPI.MSB,
            sck = Pin(device_config['sck'], Pin.OUT, Pin.PULL_DOWN),
            mosi = Pin(device_config['mosi'], Pin.OUT, Pin.PULL_UP),
            miso = Pin(device_config['miso'], Pin.IN, Pin.PULL_UP))
示例#6
0
 def create_helpers(self):
     self.led = Pin(self.LED, Pin.OUT)
     self.i2c = SoftI2C(scl=Pin(self.OLED_SCL), sda=Pin(self.OLED_SDA))
     self.oled = OLED(self.i2c)
示例#7
0
def autoAntenna():
    oled.fill(0)
    oled.show()
    freq_temp = 0
    radio_temp = 0
    ant_temp = 0
    try:
        while (True):
            if 0 == p36.value() or 0 == p39.value() or 0 == p34.value(
            ) or 0 == p35.value():
                print('Manual Antenna')
                manualAntenna()
            rssi = str(station.status('rssi'))
            graphics.fill_rect(75, 10, 128, 10, 0)
            oled.text(rssi + 'dBm', 75, 10, 1)
            oled.show()
            bytesAddressPair = s.recvfrom(bufferSize)
            message = bytesAddressPair[0]
            clientMsg = "Message from Client:{}".format(message)
            parser1 = clientMsg.split("</Freq")[0]
            freq = int(float(parser1.split("Freq>")[1]))
            parser2 = clientMsg.split("</RadioNr")[0]
            radio = int(int(parser2.split("RadioNr>")[1]))
            parser3 = clientMsg.split("</Antenna")[0]
            ant = int(int(parser3.split("Antenna>")[1]))
            #print('in Auto loop')
            if radio != radio_temp or ant != ant_temp or freq != freq_temp:
                #need to add pin change above to work outside N1MM
                freq_temp = freq
                ant_temp = ant
                radio_temp = radio
                band = 0
                #Display ham bands and band decoder
                if 179999 < freq < 200000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b160.value(1)
                    band = '160'
                    print(band + " meters band, MCU:",
                          str(b160) + " Radio#:", radio, "N1MM Antenna#:", ant)
                if 349999 < freq < 400000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b80.value(1)
                    band = '80'
                    print(band + " meters band, MCU:",
                          str(b80) + " Radio#:", radio, "Antenna#:", ant)
                if 533050 < freq < 540350:
                    band = '60'

                if 699999 < freq < 730000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b40.value(1)
                    band = '40'
                    print(band + " meters band, MCU:",
                          str(b40) + " Radio#:", radio, "Antenna#:", ant)
                if 1010000 < freq < 1015000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b30.value(1)
                    band = '30'
                    print(band + " meters band, MCU:",
                          str(b30) + " Radio#:", radio, "Antenna#:", ant)
                if 1399999 < freq < 1435000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b20.value(1)
                    band = '20'
                    print(band + " meters band, MCU:",
                          str(b20) + " Radio#:", radio, "Antenna#:", ant)
                if 1806799 < freq < 1816800:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b17.value(1)
                    band = '17'
                    print(band + " meters band, MCU:",
                          str(b17) + " Radio#:", radio, "Antenna#:", ant)
                if 2099999 < freq < 2145000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b15.value(1)
                    band = '15'
                    print(band + " meters band, MCU:",
                          str(b15) + " Radio#:", radio, "Antenna#:", ant)
                if 2489000 < freq < 2499000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b12.value(1)
                    band = '12'
                    print(band + " meters band, MCU:",
                          str(b12) + " Radio#:", radio, "Antenna#:", ant)
                if 2696500 < freq < 2740500:
                    band = 'C.B. Band'
                if 2799999 < freq < 2970000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b10.value(1)
                    band = '10'
                    print(band + " meters band, MCU:",
                          str(b10) + " Radio#:", radio, "Antenna#:", ant)
                if 4999999 < freq < 5400000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b6.value(1)
                    band = '6'
                    print(band + " meters band, MCU:",
                          str(b6) + " Radio#:", radio, "Antenna#:", ant)
                if 14399999 < freq < 14800000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b2.value(1)
                    band = '2'
                    print(band + " meters band, MCU:",
                          str(b2) + " Radio#:", radio, "Antenna#:", ant)
                if 42000000 < freq < 45000000:
                    b160.value(0),b80.value(0),b40.value(0),b30.value(0),b20.value(0),b17.value(0),\
                    b15.value(0),b12.value(0),b10.value(0),b6.value(0),b2.value(0),b70.value(0)
                    b70.value(1)
                    band = '70c'
                    print(band + " meters band, MCU:",
                          str(b70) + " Radio#:", radio, "Antenna#:", ant)
                #Display outside the ham bands
                if 3000 < freq < 27900:
                    band = 'Long Wave '
                if 27999 < freq < 150000:
                    band = 'Medium Wave '
                if 150000 <freq< 179999 or 200001 <freq< 349999 or 400001 <freq< 533049 \
                    or 540351 <freq< 699999 or 730001 <freq< 1009999 or 1015001 <freq< 1400000 \
                     or 1435001 <freq< 1806800 or 1816800 <freq< 2100000 or 2145000 <freq< 2489000 \
                      or 2499000 <freq <2696500 or 2740500 <freq< 2800000 or 2970001 <freq< 3000000:
                    band = 'ShortWave  '
                if 3000001 < freq < 4999999 or 5400001 < freq < 30000001:
                    band = 'VHF       '
                if freq > 30000001:
                    band = 'UHF       '

                if ant == 2:
                    #Position 1
                    AnT = 'LONG WIRE'
                    Pin2.on()
                    sleep(0.1)
                    Pin4.on()
                    sleep(0.1)
                    Pin5.on()

                if ant == 0:
                    #Position 2
                    AnT = 'BEAM'
                    Pin2.off()
                    sleep(0.1)
                    Pin4.off()
                    sleep(0.1)
                    Pin5.on()

                if ant == 1:
                    #Position 3
                    AnT = '40m DIPOLE'
                    Pin2.off()
                    sleep(0.1)
                    Pin4.on()
                    sleep(0.1)
                    Pin5.on()

                if ant == 3:
                    #Position 4
                    AnT = 'DUMMY LOAD'
                    Pin2.off()
                    sleep(0.1)
                    Pin4.off()
                    sleep(0.1)
                    Pin5.off()

                # OLED initialization for boot time information
                i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
                oled.fill(0)
                oled.text('Band: ' + str(band) + ' mtrs', 0, 0)
                oled.text('Ant: ' + AnT, 0, 20, 1)
                #Uncoment the line below if you want more information in auto mode
                #Make sure you coment out the RSSI line 230, 231
                #Frequency information on the OLED, too crowded for me
                #oled.text('Frq: '+ str(freq), 0, 10)
                oled.show()
    except OSError:
        print('Error')
        oled.fill(0)
        oled.text('N1MM server down', 1, 1, 1)
        oled.text('...rebooting', 0, 20, 1)
        oled.show()
        sleep(1)
        autoAntenna()
示例#8
0

def IR_Callback(data, addr, ctrl):
    if data < 0:  # NEC protocol sends repeat codes.
        print('Repeat code.')
    else:
        print('Data {:02x} Addr {:04x}'.format(data, addr))
    if data == 0x99:
        pass
    if data == 0x9a:
        pass


# test(0)

i2c = SoftI2C(scl=Pin(15), sda=Pin(4))

oleRz = Pin(16, Pin.OUT)
oleRz.value(0)
sleep(0.05)
oleRz.value(1)
oled_with = 128
oled_hight = 64
oled = SSD1306_I2C(oled_with, oled_hight, i2c)
lin_hight = 9
col_with = 8


def text_write(text, lin, col):
    oled.text(text, col * col_with, lin * lin_hight)
    "January", "February", "March", "April", "May", "June", "July", "August",
    "September", "October", "November", "December"
]
month_short = [
    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
    "Nov", "Dec"
]
day_of_week_str = [
    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
    "Sunday"
]
day_of_week_short = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
#
# initialize I2C bus 0 on the t-watch
#
t_watch_i2c0 = SoftI2C(scl=Pin(T_WATCH_I2C0_SCL), sda=Pin(T_WATCH_I2C0_SDA))
#
# create a pcf8563 object on the I2C0 bus
#
pcf8563 = PCF8563(t_watch_i2c0)
currentTime = pcf8563.datetime()

year = currentTime[0]
month = currentTime[1]
date = currentTime[2]
hour = currentTime[3]
minute = currentTime[4]
second = currentTime[5]
day = currentTime[6]
print("Time read from PCF8563: %s %d. %s %02d %02d:%02d:%02d" %
      (day_of_week_short[day], date, month_short[month], year, hour, minute,
示例#10
0
# while True:
#     w1 = IR_sensorL.read()
#     w2 = IR_sensorML.read()
#     w3 = IR_sensorMR.read()
#     w4 = IR_sensorR.read()
#
#     numerator =
#     denominator =
#
#     line_dist = numerator/denominator;
#
#     print("Distance from line = {:3.2f}".format(line_dist));
#     t.sleep(0.1);

i2c = SoftI2C(scl=Pin("PB13"), sda=Pin("PB14"))
apds9960=APDS9960LITE(i2c)

speed = 70

while True:

    IR_readingL = IR_sensorL.value()
    IR_readingML = IR_sensorML.value()
    IR_readingMR = IR_sensorMR.value()
    IR_readingR = IR_sensorR.value()
    dist = ultrasonic_sensor.distance_mm()

    motorL.set_forwards()
    motorR.set_forwards()
    motorR.duty(speed)
示例#11
0
 Beware:
 I have tested a few methods for setting up the I2C object. Only the software I2C worked, as used below.
 
 Course:
 MicroPython with the ESP32
 https://techexplorations.com

'''

from machine import Pin, SoftI2C  #I2C #SoftI2C
import ssd1306
from time import sleep

#i2c = I2C(scl=Pin(25), sda=Pin(26), freq=400000)  # Gives deprecation warning
#i2c = I2C(1)  # Does not work
i2c = SoftI2C(scl=Pin(25), sda=Pin(26),
              freq=400000)  # Using software I2C WORKS

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

while True:
    oled.fill(0)
    oled.text('Welcome', 0, 0)
    oled.text('OLED Display', 0, 10)
    oled.text('line 3', 0, 20)
    oled.text('line 4', 0, 30)
    oled.show()
    sleep(1)
    oled.fill(1)
    oled.show()
# Turn off vendor OS debugging messages
import esp
esp.osdebug(None)

# Run a garbage collector to reclaim memory occupied by objects that are no longer used by the program
# This is useful to save space in the flash memory.
import gc
gc.collect()


# With the ESP32 you can set almost any pin to have I2C capabilities.
# You just need to set that in your code.

# import sensor library and set Pins for I2C
import BME280
i2c_sensor = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
bme = BME280.BME280(i2c=i2c_sensor)

# import OLED display library, set Pins for I2C and activate a screen
import ssd1306
i2c_oled = SoftI2C(scl=Pin(32), sda=Pin(33))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_oled)


# set the ESP32 as a Wi-Fi station, activate it and make a connection with a router
from secrets import ssid, password
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
def create_i2c(pin_sda, pin_scl):
    return SoftI2C(scl=Pin(pin_scl), sda=Pin(pin_sda))
示例#14
0
# Scans all addresses on t-watch I2C bus 1 and prints the addresses of connected
# modules
# copyright U. Raich 29.9.2020
# This program is released under MIT license

from machine import Pin, SoftI2C
import sys, time

print("Scanning the I2C bus 1 on t-watch")
print("Copyright: U.Raich")
print("Released under MIT license")

scl = Pin(32)  # bus 1 on t-watch
sda = Pin(23)  #

i2c = SoftI2C(scl, sda)
addr = i2c.scan()

print("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f")

j = 0
for i in range(0, 16):
    print('%02x' % (16 * i), end=': ')
    for j in range(0, 16):
        if 16 * i + j in addr:
            print('%02x' % (16 * i + j), end=' ')
        else:
            print("--", end=' ')
    time.sleep(0.1)
    print()
示例#15
0
import machine, time, math, network, utime, json, gc
from machine import Pin, SoftI2C
import ssd1306
import urequests

wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("ssid", "password")
time.sleep(8)

i2c = SoftI2C(scl=Pin(18), sda=Pin(19), freq=100000)

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.text('abc, World 1!', 0, 0)
oled.text('def, World 2!', 0, 10)
oled.text('3Hello, World 3!', 0, 20)
oled.show()

ddd = {'Bitcoin': 'btc_btcbitstamp'}
fontn = {
    '1': [
        0x00, 0x00, 0x00, 0x08, 0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
        0x08, 0x3E, 0x00, 0x00
    ],  #"1",0*/
    '2': [
        0x00, 0x00, 0x00, 0x3C, 0x42, 0x42, 0x42, 0x02, 0x04, 0x08, 0x10, 0x20,
        0x42, 0x7E, 0x00, 0x00
    ],  #"2",1*/
    '3': [
示例#16
0
 * lcd_api.py: https://github.com/dhylands/python_lcd/blob/master/lcd/lcd_api.py
 * esp8266_i2c_lcd.py: https://github.com/dhylands/python_lcd/blob/master/lcd/esp8266_i2c_lcd.py

 Course:
 MicroPython with the ESP32
 https://techexplorations.com

'''

from machine import SoftI2C, Pin
import esp8266_i2c_lcd as esp8266_lcd  # import I2cLcd
from time import sleep

#DEFAULT_I2C_ADDR = 0x27

i2c = SoftI2C(scl=Pin(4), sda=Pin(0), freq=400000)  # Using software I2C

lcd = esp8266_lcd.I2cLcd(i2c, esp8266_lcd.DEFAULT_I2C_ADDR, 2, 16)

lcd.clear()

counter = 0

while True:
    lcd.move_to(0, 0)
    lcd.putstr("2x16 LCD demo")
    lcd.move_to(0, 1)

    counter = counter + 1
    lcd.putstr("Counter: %d" % (counter))
    print("Counter: %d" % (counter))
示例#17
0
#导入相关模块
import mpu6050,time
from machine import SoftI2C,Pin
from tftlcd import LCD43M


#定义常用颜色
WHITE=(255,255,255)
BLACK = (0,0,0)

#初始化LCD
d=LCD43M()
d.fill(WHITE)#填充白色

#MPU6050初始化,这里使用软件I2C
i2c = SoftI2C(sda=Pin("B9"), scl=Pin("B8"))
accelerometer = mpu6050.accel(i2c)

#显示标题
d.printStr('01Studio MPU6050', 40, 10, BLACK, size=4)

while True:

    #获取传感器信息
    value=accelerometer.get_values()

    #显示加速度数据
    d.printStr('Ac-X:'+str(value["AcX"]), 10, 100, BLACK, size=4)
    d.printStr('Ac-Y:'+str(value["AcY"]), 10, 150, BLACK, size=4)
    d.printStr('Ac-Z:'+str(value["AcZ"]), 10, 200, BLACK, size=4)
示例#18
0
 def init_i2c(self):
     if self.bus is None:
         print('* initializing i2c')
         self.bus = SoftI2C(scl=self.pin_scl, sda=self.pin_sda)
示例#19
0
#If you need to change the port from default of 12060, change it below and in N1MM
localPort = 12060
bufferSize = 1024
msgFromServer = "Hello UDP Client"
bytesToSend = str.encode(msgFromServer)

addrs = (IP, localPort)
s = socket(AF_INET, SOCK_DGRAM)
s.settimeout(15)
s.bind(addrs)
#The line below is for serial output debugging
print("N1MM UDP server up and listening on port", IP)

# Only uncomment one of the lines below based on your hardware
# ESP32 Pin assignment
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
# OLED initialization for boot time information
# Dispalys logo/name and IP info to add to N1MM
# Even if you use a 128x64 display leave height at 32 because it looks bigger for us old folks
oled_width = 128
oled_height = 32
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# Raspbery Pi Logo as 32x32 array for future testing, loads on boot skewed
buffer = bytearray(
    b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
# Load the raspberry pi logo into the framebuffer (the image is 32x32)
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
# Blit the image from the framebuffer to the oled display
# oled.blit(fb, 38, 1)
# oled.show()
示例#20
0
pwm0.freq(1000)  # set frequency
pwm0.duty_u16()  # get current duty cycle, range 0-65535
pwm0.duty_u16(200)  # set duty cycle, range 0-65535
pwm0.deinit()  # turn off PWM on the pin

# ADC
from machine import ADC, Pin

adc = ADC(Pin(26))  # create ADC object on ADC pin
adc.read_u16()  # read value, 0-65535 across voltage range 0.0v - 3.3v

# Software I2C

from machine import Pin, SoftI2C

i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=100_000)

i2c.scan()  # scan for devices

i2c.readfrom(0x3A, 4)  # read 4 bytes from device with address 0x3a
i2c.writeto(0x3A, "12")  # write '12' to device with address 0x3a

buf = bytearray(10)  # create a buffer with 10 bytes
i2c.writeto(0x3A, buf)  # write the given buffer to the peripheral

# Hardware I2C
from machine import Pin, I2C

i2c = I2C(0)  # default assignment: scl=Pin(9), sda=Pin(8)
i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=400_000)
示例#21
0
'''
实验名称:土壤湿度传感器
版本:v1.0
日期:2021.1
作者:01Studio 【www.01Studio.org】
说明:通过土壤湿度传感器对土壤湿度测量并显示。
'''

#导入相关模块
import time
from machine import Pin,SoftI2C,ADC
from ssd1306 import SSD1306_I2C

#初始化oled
i2c = SoftI2C(scl=Pin(10), sda=Pin(11))   #SoftI2C初始化:scl--> 10, sda --> 11
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c) #OLED显示屏初始化:128*64分辨率,OLED的I2C地址是0x3c

#初始化ADC1,Pin=27
Soil = ADC(1)

while True:

	oled.fill(0)  # 清屏显示黑色背景
	oled.text('01Studio', 0, 0)  # 首行显示01Studio
	oled.text('Soil test:', 0, 15)      # 次行显示实验名称

	value=Soil.read_u16() #获取ADC数值

    #显示数值
 	oled.text(str(value)+' (65535)',0,40)
 	#计算电压值,获得的数据0-4095相当于0-3V,('%.2f'%)表示保留2位小数
示例#22
0
def main():
    '''
    Draw on screen using focaltouch sensor
    '''
    try:
        # Turn on display backlight
        axp = axp202c.PMU()
        axp.enablePower(axp202c.AXP202_LDO2)

        # initialize display spi port
        spi = SPI(1,
                  baudrate=32000000,
                  polarity=1,
                  phase=0,
                  bits=8,
                  firstbit=0,
                  sck=Pin(18, Pin.OUT),
                  mosi=Pin(19, Pin.OUT))

        # configure display
        tft = st7789.ST7789(spi,
                            240,
                            240,
                            cs=Pin(5, Pin.OUT),
                            dc=Pin(27, Pin.OUT),
                            backlight=Pin(12, Pin.OUT),
                            rotation=2)

        # enable display and clear screen
        tft.init()
        tft.fill(st7789.BLACK)
        tft.text(font, "Draw", 104, 1, st7789.WHITE)

        # enable focaltouch touchscreen
        touch_i2c = SoftI2C(scl=Pin(32), sda=Pin(23))
        touch = focaltouch.FocalTouch(touch_i2c)

        color_index = 0
        color = 0
        # draw color swatches used to select color to draw
        for color_index, color in enumerate(COLORS):
            tft.fill_rect(color_index * 30, 210, 30, 30, color)

        add_highlight(tft, color_index)
        while True:
            # can be up to two touches
            if touch.touched == 1:

                # get x and y points of the first touch
                p_x = touch.touches[0]['x']
                p_y = touch.touches[0]['y']

                # If point is in the lowest 30 rows of the screen
                # change color to swatch pressed.
                if p_y > 209:
                    # remove highlight from around previous color swatch
                    remove_highlight(tft, color_index, color)

                    # update new color
                    color_index = p_x // 30
                    color = COLORS[color_index]

                    add_highlight(tft, color_index)
                else:
                    # draw the pixel - would be better with lines
                    tft.pixel(p_x, p_y, color)

    finally:
        # shutdown spi
        spi.deinit()

        # turn off display backlight
        axp.disablePower(axp202c.AXP202_LDO2)
from ssd1306 import SSD1306_I2C
import urandom, utime, gc

freq(160000000)
gc.enable()
urandom.seed(sum([ADC(0).read() for _ in range(1000)
                  ]))  # generate randomize seed from floating analog pin

X = WIDTH // DOT_SIZE
Y = HEIGHT // DOT_SIZE
TOTAL = X * Y
board = [0 if urandom.getrandbits(RAND_BIT) else 1 for _ in range(TOTAL)]
gen = 0

display = SSD1306_I2C(WIDTH, HEIGHT,
                      SoftI2C(scl=Pin(5), sda=Pin(4), freq=400000))
display.fill(0)
display.show()

print('Conway\'s Game of Life: matrix size {} x {}'.format(X, Y))


def calculate_next_gen():  # calculate next generation of cells
    global board
    buffer = [0] * TOTAL
    for i in range(TOTAL):
        group = board[i-1:i+2] + \
                board[(i-1-X)%TOTAL:(i+2-X)%TOTAL] + \
                board[(i-1+X)%TOTAL:(i+2+X)%TOTAL]
        cells = sum(group)
        if not board[i]:
pw2=PWM(p2,freq=1,duty=256)

v_mode=set_mode()

pw2.freq(10)
pw2.duty(512)


#socket used to send segments 
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

#Pin related to VSYNC
p4=Pin(16,Pin.IN)

#I2C for CCI
i2c=SoftI2C(sda=Pin(21),scl=Pin(22))

#initialisation : 1) reboot to ensure an operational state & to get the default parameters (in particular if the ESP32 is reset through EN) 2) enables AGC & disables RAD 3) GPIO Mode VSYNC    -> grayscale on 8 bits 
check_status_bit()
check_busy()
write_command((0x800,0x40),'RUN')
utime.sleep_ms(5200)

check_status_bit()
check_busy()
check_SYS_FFC()

# AGC
if v_mode=='L':
    enable_AGC()
示例#25
0
# Complete project details at https://RandomNerdTutorials.com

from machine import Pin, SoftI2C
import ssd1306
from time import sleep

# ESP32 Pin assignment
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))

# ESP8266 Pin assignment
#i2c = SoftI2C(scl=Pin(5), sda=Pin(4))

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

oled.text('Hello, World 1!', 0, 0)
oled.text('Hello, World 2!', 0, 10)
oled.text('Hello, World 3!', 0, 20)

oled.show()
示例#26
0
'''
实验名称:大气压强传感器
版本:v1.0
日期:2021.1
作者:01Studio 【www.01Studio.org】
说明:测量BMP280温度、气压和计算海拔值,并在OLED上显示。。
'''

import time,bmp280
from machine import Pin,SoftI2C
from ssd1306 import SSD1306_I2C

#初始化oled
i2c1 = SoftI2C(scl=Pin(10), sda=Pin(11))   #软件I2C初始化:scl--> 10, sda --> 11
oled = SSD1306_I2C(128, 64, i2c1, addr=0x3c) #OLED显示屏初始化:128*64分辨率,OLED的I2C地址是0x3c

#初始化BMP280,软件模拟I2C
i2c2 = SoftI2C(scl=Pin(4), sda=Pin(5))   #软件I2C初始化:scl--> 4, sda --> 5
BMP = bmp280.BMP280(i2c2)

while True:

    oled.fill(0)  # 清屏,背景黑色
    oled.text('01Studio', 0, 0)
    oled.text('Air Pressure:', 0, 15)

    # 温度显示
    oled.text(str(BMP.getTemp()) + ' C', 0, 35)
    # 湿度显示
    oled.text(str(BMP.getPress()) + ' Pa', 0, 45)
    # 海拔显示