prjbrook

digikey4plusSD1P.py PIco+SD second go

Sep 19th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #Combined digikey4 with Tom0Oled to get CD card reader and OLED display in one program. Worked 4:28,30/07/24
  2. #first, do imports for SD card
  3. print("Simple SD card testDigiKey1.1 ")  #Tue Jun 28 17:09:24 NZST 2022
  4. print("Pi Pico and Code with Thonny.")
  5. import machine
  6. import sdcard
  7. import os  #why not uos?
  8.  
  9. #Next lines are for CD card. Originally from digikey4
  10. from machine import Pin, I2C #12:04, 30/07/24 Worked
  11. from ssd1306 import SSD1306_I2C
  12.  
  13. i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
  14. oled = SSD1306_I2C(128, 64, i2c)
  15.  
  16. oled.text("Tom's Hardware7Oct", 0, 0)
  17. oled.show()  
  18.  
  19.  
  20. # Assign chip select (CS) pin (and start it high)  
  21. cs = machine.Pin(9, machine.Pin.OUT)  #does this really start high?
  22.  
  23. # Intialize SPI peripheral (start with 1 MHz) Check it IS 1000000
  24. spi = machine.SPI(1,
  25.                   baudrate=100000,
  26.                   polarity=0,
  27.                   phase=0,
  28.                   bits=8,
  29.                   firstbit=machine.SPI.MSB,
  30.                   sck=machine.Pin(10),
  31.                   mosi=machine.Pin(11),
  32.                   miso=machine.Pin(8))
  33.  
  34. # Initialize SD card
  35. sd = sdcard.SDCard(spi, cs)
  36.  
  37. vfs = os.VfsFat(sd)
  38. os.mount(vfs, "/sd")
  39.  
  40. # Create a file and write something to it
  41. with open("/sd/test03.txt", "w") as file:
  42.     file.write("Hello,WWWWORX22345 69!! SD World!\r\n")
  43.     file.write("This is a test and seems to work@@022 ABCDEfg Fri Sep 20 11:25:39 NZST 2024\r\n")
  44.  
  45. # Open the file we just created and read from it
  46. with open("/sd/test03.txt", "r") as file:
  47.     data = file.read()
  48.     print(data)
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment