--- disqus: ahb0222 GA : G-VF9ZT413CG --- # micropython SSD1306使用紀錄 > [color=#40f1ef][name=LHB阿好伯, 2021/07/16][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `Micropython` [TOC] https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py ![](https://hackmd.io/_uploads/HJir9kZO5.png) ![](https://hackmd.io/_uploads/ByaO5kZ_c.png) 上傳到micropython裝置 ![](https://hackmd.io/_uploads/BJZ7hybO9.png) ![](https://hackmd.io/_uploads/rJUhKJZOq.png) ```python= from machine import Pin, I2C, SoftI2C import ssd1306 import time # using default address 0x3C # i2c = I2C(sda=Pin(23), scl=Pin(19)) #硬體 I2C i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) #慧手科技套件腳位 #i2c = SoftI2C(scl=Pin(19), sda=Pin(23), freq=400000) # 軟體 I2C display = ssd1306.SSD1306_I2C(128, 64, i2c) display.invert(0) # 螢幕顏色正常 #display.invert(1) # 螢幕顏色反轉 display.rotate(True) # 螢幕旋轉180度 #display.rotate(False) # 螢幕旋轉0度 display.text('Hello, World~~~', 0, 0, 1) display.show() # 螢幕顯示 display.fill(0) # 填充螢幕 0 = 全黑, 1 = 全亮 time.sleep(1) display.show() # 螢幕顯示 display.pixel(0, 10) # 取得像素資訊(x, y) time.sleep(1) display.show() # 螢幕顯示 display.pixel(0, 10, 1) # 取得像素(x, y, colour) colour=1為亮, 0 為滅 time.sleep(1) display.show() # 螢幕顯示 display.hline(0, 8,100, 1) # 繪製水平線( x, y, width, colour) width寬度 colour=1為亮, 0 為滅 time.sleep(1) display.show() # 螢幕顯示 display.vline(0, 8, 4, 1) # 繪製垂直線( x, y, height, colour) height高度 colour=1為亮, 0 為滅 time.sleep(1) display.show() # 螢幕顯示 display.line(0, 0, 127, 63, 1) # 繪製線條(x1,y1, x2, y2, colour)colour=1為亮, 0 為滅 time.sleep(1) display.show() # 螢幕顯示 display.rect(0, 0, 127, 63, 1) # 繪製矩形輪廓(x1,y1, x2, y2, colour) colour=1為亮, 0 為滅 time.sleep(1) display.show() # 螢幕顯示 display.fill_rect(0, 0, 127, 63, 1) # 繪製填充矩形(x1,y1, x2, y2, colour) colour=1為亮, 0 為滅 time.sleep(1) display.show() # 螢幕顯示 display.text('Hello World', 0, 0, 1) # 繪製文字(test, x, y, colour) colour=1為亮, 0 為滅 time.sleep(1) display.show() # 螢幕顯示 display.scroll(20, 0) # 向右偏移scroll 20 pixels to the right time.sleep(1) display.show() # 螢幕顯示 ``` # 繪製圖形套件_gfx https://github.com/adafruit/micropython-adafruit-gfx/blob/master/gfx.py ```python= from machine import Pin, I2C, SoftI2C import ssd1306 import time import gfx # using default address 0x3C # i2c = I2C(sda=Pin(23), scl=Pin(19)) #硬體 I2C i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) #慧手科技套件腳位 #i2c = SoftI2C(scl=Pin(19), sda=Pin(23), freq=400000) # 軟體 I2C display = ssd1306.SSD1306_I2C(128, 64, i2c) graphics = gfx.GFX(128, 64, display.pixel) display.invert(0) # 螢幕顏色正常 #display.invert(1) # 螢幕顏色反轉 # display.rotate(True) # 螢幕旋轉180度 display.rotate(False) # 螢幕旋轉0度 display.text('Hello, World~~~', 0, 0, 1) display.show() # 螢幕顯示 display.fill(0) # 填充螢幕 0 = 全黑, 1 = 全亮 time.sleep(1) display.show() # 螢幕顯示 graphics.line(0, 0, 127, 63, 1) #line(x0, y0, x1, y1, color) display.show() time.sleep(1) graphics.rect(0, 0, 128, 64, 1) #rect(x0, y0, width, height, color) display.show() #fill_rect(x0, y0, width, height, color) #填充矩形 time.sleep(1) graphics.circle(64, 32, 10, 1) #circle(x0, y0, radius半徑, color) (x0, y0) 座標表示圓的中心 display.show() #fill_circle(x0, y0, radius, color) #填充圓 time.sleep(1) graphics.triangle(64, 0,0,64,128,64,1) #triangle(x0, y0, x1, y1, x2, y2, color) 每個角的座標 display.show() #fill_triangle(x0, y0, x1, y1, x2, y2, color) #填充三角形 ``` ## 數值滑塊應用 ![](https://hackmd.io/_uploads/rkL-8Tgtq.jpg) ```python= from machine import Pin, I2C, SoftI2C, ADC import ssd1306 import time import gfx import math adc_pin = Pin(32) adc = ADC(adc_pin) # 創建作用在PIN上的ADC對象 #val_2 = adc.read_uv() # 讀取微伏值 adc.atten(ADC.ATTN_11DB) # using default address 0x3C # i2c = I2C(sda=Pin(23), scl=Pin(19)) #硬體 I2C i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) #慧手科技套件腳位 #i2c = SoftI2C(scl=Pin(19), sda=Pin(23), freq=400000) # 軟體 I2C display = ssd1306.SSD1306_I2C(128, 64, i2c) graphics = gfx.GFX(128, 64, display.pixel) display.invert(0) # 螢幕顏色正常 #display.invert(1) # 螢幕顏色反轉 # display.rotate(True) # 螢幕旋轉180度 display.rotate(False) # 螢幕旋轉0度 display.text(' ~~~Slider~~~', 0, 0, 1) display.show() # 螢幕顯示 def map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) graphics.rect(10, 32, 100, 10, 1) #rect(x0, y0, width, height, color) display.show() val_3 = 0 while True: val_1 = adc.read() # 讀取0-4095中讀原始模擬值 print("val_1:",val_1) val_2 = map(val_1,0,4095,0, 100) if val_3 != val_2 : display.fill_rect(11, 33, 98, 8, 0) #刪除填充 display.fill_rect(0, 19, 128, 13, 0) #刪除數字三角 display.show() display.fill_rect(10, 32, val_2, 10, 1) graphics.triangle(val_2-3+10, 27,val_2+2+10,27,val_2+10,31,1)#triangle(x0, y0, x1, y1, x2, y2, color) 每個角的座標 display.text(str(val_2), val_2+5, 19, 1) display.show() val_3 = val_2 time.sleep_ms(500) ``` ![](https://hackmd.io/_uploads/ByUGU6gK5.jpg) ```python= from machine import Pin, I2C, SoftI2C, ADC import ssd1306 import time import gfx import math adc_pin = Pin(32) adc = ADC(adc_pin) # 創建作用在PIN上的ADC對象 #val_2 = adc.read_uv() # 讀取微伏值 adc.atten(ADC.ATTN_11DB) # using default address 0x3C # i2c = I2C(sda=Pin(23), scl=Pin(19)) #硬體 I2C i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) #慧手科技套件腳位 #i2c = SoftI2C(scl=Pin(19), sda=Pin(23), freq=400000) # 軟體 I2C display = ssd1306.SSD1306_I2C(128, 64, i2c) graphics = gfx.GFX(128, 64, display.pixel) display.invert(0) # 螢幕顏色正常 #display.invert(1) # 螢幕顏色反轉 # display.rotate(True) # 螢幕旋轉180度 display.rotate(False) # 螢幕旋轉0度 display.text(' ~~~Slider2~~~', 0, 0, 1) display.show() # 螢幕顯示 def map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) display.show() val_3 = 0 while True: val_1 = adc.read() # 讀取0-4095中讀原始模擬值 print("val_1:",val_1) val_2 = map(val_1,0,4095,0, 100) if val_3 != val_2 : display.fill_rect(val_2+13, 33, 103, 2, 0) #刪除填充 # display.fill_rect(11, 33, 103, 2, 0) #刪除填充 display.fill_rect(5, 36, 130, 2, 0) #刪除底部 display.fill_rect(5, 19, 130, 13, 0) #刪除頂部 display.fill_rect(110, 32, 20, 5, 0) #刪除尾部 graphics.rect(10, 32, 100, 4, 1) #rect(x0, y0, width, height, color) display.show() display.fill_rect(10, 32, val_2, 3, 1) graphics.fill_circle(val_2+10, 33, 4, 1) #circle(x0, y0, radius半徑, color) (x0, y0) 座標表示圓的中心 display.text(str(val_2), val_2+5, 19, 1) val_3 = val_2 display.show() time.sleep_ms(500) ``` ## 圖片轉換工具 https://luckyresistor.me/applications/micropython-bitmap-tool/ ## 文字轉圖片工具 https://www.chineseconverter.com/zh-cn/convert/chinese-text-to-image 🌟全文可以至下方連結觀看或是補充 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/nHEcVmm.jpg)