changed 3 years ago
Linked with GitHub

[社團]mPython讀書會

mPython基本課程

本講義網址
http://gg.gg/ncbjp

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

麵包板

[基本]https://kknews.cc/zh-tw/news/pqprzqj.html
轉自台灣MicroPython 陳會安老師

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

AnalogRead應用

[ADC參考資源]http://www.1zlab.com/wiki/micropython-esp32/adc/

ADC用法

from machine import ADC

adc = ADC(Pin(32))          # create ADC object on ADC pin
adc.read()                  # read value, 0-4095 across voltage range 0.0v - 1.0v

adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc.width(ADC.WIDTH_9BIT)   # set 9 bit return values (returned range 0-511)
adc.read()                  # read value using the newly configured attenuation and width

PWM法用

from machine import Pin, PWM

pwm0 = PWM(Pin(0))      # create PWM object from a pin
pwm0.freq()             # get current frequency
pwm0.freq(1000)         # set frequency
pwm0.duty()             # get current duty cycle
pwm0.duty(200)          # set duty cycle
pwm0.deinit()           # turn off PWM on the pin

pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go

可變電阻

光敏電阻

from machine import Pin, PWM, ADC
from time import sleep

adc = ADC(Pin(32))          # create ADC object on ADC pin

adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.3v)
adc.width(ADC.WIDTH_12BIT)   # set 12 bit return values (returned range 0-1023)
i = 0

frequency = 5000
PWM_led = PWM(Pin(4), frequency)
duty_cycle = 0

while True:
    i = adc.read()
    print("value =",i)
    V = i/4096*3.3 #Caculate the true voltage
    print("V = ", V)
    if V < 2:
        duty_cycle = 0
    else:
        duty_cycle = 1023
        
    PWM_led.duty(duty_cycle)
    sleep(0.25)


調光器 x WS2812

測試參考

https://docs.singtown.com/micropython/zh/latest/esp32/esp32/tutorial/neopixel.html
https://mc.dfrobot.com.cn/thread-305498-1-1.html


接線方法

WS2812 DIN -> D4
可變電阻中間角 -> D2


控制方法

from machine import Pin
from neopixel import NeoPixel

pin = Pin(0, Pin.OUT)   # set GPIO0 to output to drive NeoPixels
np = NeoPixel(pin, 8)   # create NeoPixel driver on GPIO0 for 8 pixels
np[0] = (255, 255, 255) # set the first pixel to white
np.write()              # write data to all pixels
r, g, b = np[0]         # get first pixel colour

Servo Motor 伺服馬達

接線方法

Servo Motor控制訊號為D4(在此接線圖情況下才成立)
輸入訊號為D2(電阻改變 產生電壓改變 進一步控制Servo Motor的PWM數值)

伺服馬達應用

  1. 先使用PWM語法讓馬達可以順利轉動到某個位置
  2. 再設計For迴圈 讓馬達可以達到0度自動轉到180度的效果
  3. 進一步搭配之前的可變電阻當作輸入,依照轉動的大小,馬達轉動到對應位置

測試範例

參考資料| ESP32 教學 | MicroPython | PWM Control | 202 |

import machine
import time

#设置PWM 引脚G5,频率50Hz
servo = machine.PWM(machine.Pin(5), freq=50)

servo.duty(40)#舵机角度的设定
time.sleep(2)#延时2秒
servo.duty(115)
time.sleep(2)
servo.duty(180)

ESP32 v.s. Servo

from machine import Pin,PWM
import time
sg_pin=PWM(Pin(14),freq=50,duty=0)  #duty range 0-1023
d_90=(int)(1023*0.0725) #1.45ms
d_zero=(int)(1023*0.025) #0.5ms
d_180=(int)(1023*0.12)  #2.4ms
de_map=[d_90,d_zero,d_90,d_180]
try:
    while 1:
        for i in de_map:
            sg_pin.duty(i)
            time.sleep(1)
except Exception as e:
    print(e)
    sg_pin.deinit()

MPU6050 I2C 三軸陀螺儀加速計感測模組

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接線方法

VCC 選3.3V
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=)

應用

測試範例

from machine import I2C, Pin
import mpu6050, time
i2c = I2C(scl=Pin(22), sda=Pin(21))
acc = mpu6050.accel(i2c)
count = 600
delta = 0.1
sum_x, sum_y, sum_z = 0, 0, 0
for i in range(count):
	time.sleep(delta)
	d = acc.get_values()
	sum_x += d['GyX']
	sum_y += d['GyY']
	sum_z += d['GyZ']
avg_x = float(sum_x) / count
avg_y = float(sum_y) / count
avg_z = float(sum_z) / count

with open("avg_values.txt", "wt", encoding='utf-8') as fp:
	fp.write("x:{}, y:{}, z:{}\n".format(avg_x, avg_y, avg_z))

OLED I2C 螢幕

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接線方法

VCC 選3.3V
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=)

應用

power consumption1
power consumption2
約耗20-30mA 約0.06W

測試範例

顯示訊息程式:

from machine import I2C, Pin
import ssd1306
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

oled.fill(0)
oled.text("Hello world!", 0, 0)
oled.show()

底下的程式碼是和溫濕度感測器一起工作的例子

import dht, time
from machine import I2C, Pin
import ssd1306
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
d = dht.DHT11(Pin(0))

while True:
	d.measure()
	oled.fill(0)
	oled.text("{},{}%".format(d.temperature(), d.humidity()), 0, 0)
	oled.show()
	time.sleep(0.5)

NodeMCU與WiFi


把資料傳送到ThingSpeak.com


MQTT

GitHub


資料傳送給LINE


即時繪製數據圖檔

Select a repo