传感器课程设计,老师布置做风速传感器。要求测0.5m/s-30m/s的风速,选用风杯式风速传感器,用ESP32片内ADC。关于风嘛,哪里有30m/s的风给我测哦,就拿直流电机模拟一个算了。ESP32读取外接电位器的电压值,用这个电压控制输出PWM波的占空比,送到TB6612电机驱动模块去控制风扇转速。风扇吹动风速传感器,带动内部直流发电机发电,ADC读这个电压,经过标定就知道风速了(模拟的)。
from machine import Pin, PWM, ADC, SoftI2C, Timer
import utime
from font import Font
from sd1306 import SSD1306_I2C, SSD1306
import bluetooth
#OLED部分
i2c = SoftI2C(scl=Pin(2), sda=Pin(1))
display= SSD1306_I2C(128, 32, i2c)
oled=Font(display)
#PWM
PWMOut = PWM(Pin(21)) #LED&驱动电机PWM
PWMOut.freq(1000)
#AD
Uin=ADC(Pin(11)) #电位器AD
Wind=ADC(Pin(10)) #风速传感器AD
Uin.width(ADC.WIDTH_12BIT) #读取的电压转为0-4096;ADC.WIDTH_9BIT:0-511
Uin.atten(ADC.ATTN_11DB) #衰减设置范围:无衰减
Wind.width(ADC.WIDTH_12BIT) #读取的电压转为0-4096;ADC.WIDTH_9BIT:0-511
Wind.atten(ADC.ATTN_0DB) #衰减设置范围:无衰减
#Wind_factor = 1.1 / (65535)
Resistor_factor = 3.3 / (65535)
#蓝牙部分
BLE_MSG = ""
class ESP32_BLE():
def __init__(self, name):
self.led = Pin(36,Pin.OUT)
self.timer1 = Timer(0)
self.name = name
self.ble = bluetooth.BLE()
self.ble.active(True)
self.ble.config(gap_name=name)
self.disconnected()
self.ble.irq(self.ble_irq)
self.register()
self.advertiser()
def connected(self):
self.led.value(0)
self.timer1.deinit()
def disconnected(self):
# self.timer1.init(period=100, mode=Timer.PERIODIC, callback=lambda t: self.led.duty(500))
self.led.value(1)
def ble_irq(self, event, data):
global BLE_MSG
if event == 1: #_IRQ_CENTRAL_CONNECT 手机链接了此设备
self.connected()
elif event == 2: #_IRQ_CENTRAL_DISCONNECT 手机断开此设备
self.advertiser()
self.disconnected()
elif event == 3: #_IRQ_GATTS_WRITE 手机发送了数据
buffer = self.ble.gatts_read(self.rx)
BLE_MSG = buffer.decode('UTF-8').strip()
def register(self):
service_uuid = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
reader_uuid = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
sender_uuid = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
services = (
(
bluetooth.UUID(service_uuid),
(
(bluetooth.UUID(sender_uuid), bluetooth.FLAG_NOTIFY),
(bluetooth.UUID(reader_uuid), bluetooth.FLAG_WRITE),
)
),
)
((self.tx, self.rx,), ) = self.ble.gatts_register_services(services)
def send(self, data):
self.ble.gatts_notify(1, self.tx, data + '\n')
def advertiser(self):
name = bytes(self.name, 'UTF-8')
adv_data = bytearray('\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name
self.ble.gap_advertise(100, adv_data)
print(adv_data)
print("\r\n")
def calculate_average(numbers):
if len(numbers) < 3:
return None # 返回None表示无法计算平均值
# numbers = [float(num) for num in numbers.split(",")]
numbers.remove(max(numbers))
numbers.remove(min(numbers))
average = sum(numbers) / len(numbers)
return average
def Get_list():
values = [] # 创建一个空列表用于存储值
for i in range(0,30,1):
utime.sleep_ms(2)
ValueWind = Wind.read_u16() # 风速AD
Wind_voltage = Wind.read_u16() * 1.1 / 65535
value = float(Wind_voltage)
values.append(value)
return values #返回包含30个风速电压值的列表
def Wind_Speed():
Number_List=Get_list()
Vaverage=calculate_average(Number_List)
if Vaverage >= 0.01 and Vaverage <= 0.096: #1
WSpeed=37.02*Vaverage+0.3089
elif Vaverage > 0.096 and Vaverage <= 0.166: #2
WSpeed=99.98*Vaverage-5.446
elif Vaverage > 0.166 and Vaverage <= 0.175: #3
WSpeed=42.41*Vaverage+4.529
elif Vaverage > 0.175 and Vaverage <= 0.208: #4
WSpeed=154.5*Vaverage-14.23
elif Vaverage > 0.208 and Vaverage <= 0.255: #5
WSpeed=151.5*Vaverage-14.27
elif Vaverage > 0.255: #6
WSpeed=269.1*Vaverage-42.47
else:
WSpeed=0
return WSpeed
ble = ESP32_BLE("ESP32S3_WindSpeed")
led = Pin(36)
warning_counter=0
while True:
Number_List=Get_list()
Vaverage=calculate_average(Number_List) #风速传感器输出平均电压
VGet=Wind_Speed()
for i in range(0,1000,1):
utime.sleep_ms(1)
ValueResistor=Uin.read_u16() #读电位器
PWMOut.duty_u16(ValueResistor) #输出给风扇
Ur=ValueResistor*Resistor_factor#电位器电压
VDisplay=round(VGet,2)
if BLE_MSG == 'Getwindspeed': #手机蓝牙发送的信息
BLE_MSG = "" #Message清空
ble.send(str(VDisplay)) #发送风速到手机蓝牙
if i==999:
if VDisplay == 0:
oled.text('0.000', 0, 5, 24)
oled.text('m/s', 80, 5, 24)
oled.show()
else:
if VDisplay >=30:
warning_counter += 1
if warning_counter >= 5:
ble.send('Warning')
warning_counter = 0
else:
warning_counter = 0
oled.text('{:.3f}'.format(VDisplay), 0, 5, 24)
oled.text('m/s', 80, 5, 24)
oled.show()
print("电位器电压:{:.3f}V,传感器电压:{:.3f}V".format(Ur, Vaverage)) # Convert Ur and Vaverage to strings with 3 decimal places
OLED部分,用了大字体的程序,参考自oled屏自定义大小字体 micropython 使用 - 哔哩哔哩
在此谢谢大佬。