import machine, os,uos
from machine import Pin, I2C,SPI
import ssd1306
import sdcard
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.text('Hello, Wokwi!', 10, 10)
oled.show()
# print("sdcard test...")
# sd = machine.SDCard(slot=2, width=1, sck=18, miso=19, mosi=23, cs=5)
# print("sdcard test 1...")
# uos.mount(sd, "/sd")
# print("sdcard test end...")
# Assign chip select (CS) pin (and start it high)
cs = machine.Pin(5, machine.Pin.OUT)
# Intialize SPI peripheral (start with 1 MHz)
spi = machine.SPI(2,
baudrate=1000000,
polarity=0,
phase=0,
bits=8,
firstbit=machine.SPI.MSB,
sck=machine.Pin(18),
mosi=machine.Pin(23),
miso=machine.Pin(19))
# Initialize SD card
sd = sdcard.SDCard(spi, cs)
# Mount filesystem
# vfs = uos.VfsFat(sd)
uos.mount(vfs, "/sd")
# Create a file and write something to it
with open("/sd/test01.txt", "w") as file:
file.write("Hello, SD World!\r\n")
file.write("This is a test\r\n")
# Open the file we just created and read from it
with open("/sd/test01.txt", "r") as file:
data = file.read()
print(data)