pi pico OLED, SD card combined (Version 2)

NB pi pico pinout at the bottom.

Fri Sep 20 11:25:39 NZST 2024 changed one of the lines and ran 

digikey4pluSD1P.py again. Worked. Now going to put this program into pi pico with wifi (ie picoW) and replace previous pico.

 Wrote code for SD card and for OLED and put them into one program here in Pastebin

Here's the Thonny micropython  Code: (OLED stuff from Tom's Hardware

(The SD card code and wire-up came from Digikey  
#Combined digikey4 with Tom0Oled to get CD card reader and OLED display in one program. Worked 4:28,30/07/24
#first, do imports for SD card
print("Simple SD card testDigiKey1.1 ")  #Tue Jun 28 17:09:24 NZST 2022 
print("Pi Pico and Code with Thonny.")
import machine
import sdcard 
import os  #why not uos?

#Next lines are for CD card. Originally from digikey4
from machine import Pin, I2C #12:04, 30/07/24 Worked
from ssd1306 import SSD1306_I2C

i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)

oled.text("Tom's Hardware69", 0, 0)
oled.show()  


# Assign chip select (CS) pin (and start it high)  
cs = machine.Pin(9, machine.Pin.OUT)  #does this really start high?

# Intialize SPI peripheral (start with 1 MHz) Check it IS 1000000 
spi = machine.SPI(1,
                  baudrate=100000,
                  polarity=0,
                  phase=0,
                  bits=8,
                  firstbit=machine.SPI.MSB,
                  sck=machine.Pin(10),
                  mosi=machine.Pin(11),
                  miso=machine.Pin(8))

# Initialize SD card
sd = sdcard.SDCard(spi, cs)

vfs = os.VfsFat(sd)
os.mount(vfs, "/sd")

# Create a file and write something to it
with open("/sd/test03.txt", "w") as file:
    file.write("Hello,WWWWORX22345 69!! SD World!\r\n")
    file.write("This is a test and seems to work@@022 ABCDEfg\r\n")

# Open the file we just created and read from it
with open("/sd/test03.txt", "r") as file:
    data = file.read()
    print(data)

--------------------------------------

-----------below test03.txt -----had these two lines-------------
Hello,WWWWORX22345 69!! SD World!
This is a test and seems to work@@022 ABCDEfg



  
Then wrote combined pi pico micropython program to output data on SD card, OLED display and DS18b20 one-wire temperature sensor.
Went well. Here's the combined uPy program:
#all three sensors, OLED, SD, DS18b20 Thu Aug  1 11:24:10 NZST 2024 Worked. 
#Combined digikey4 with Tom0Oled to get CD card reader and OLED display in one program. Worked 4:28,30/07/24.
#first, do imports for SD card
print("Simple SD card testDigiKey1.1 ")  #Tue Jun 28 17:09:24 NZST 2022 
print("Pi Pico and Code with Thonny.")
import machine
import sdcard 
import os  #why not uos?
import onewire, ds18x20, time


#Next lines are for CD card. Originally from digikey4
from machine import Pin, I2C #12:04, 30/07/24 Worked
from ssd1306 import SSD1306_I2C

i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)

oled.text("Tom's Hardware69", 0, 0)
oled.show()  


# Assign chip select (CS) pin (and start it high)  
cs = machine.Pin(9, machine.Pin.OUT)  #does this really start high?

# Intialize SPI peripheral (start with 1 MHz) Check it IS 1000000 
spi = machine.SPI(1,
                  baudrate=100000,
                  polarity=0,
                  phase=0,
                  bits=8,
                  firstbit=machine.SPI.MSB,
                  sck=machine.Pin(10),
                  mosi=machine.Pin(11),
                  miso=machine.Pin(8))

# Initialize SD card
sd = sdcard.SDCard(spi, cs)

vfs = os.VfsFat(sd)
os.mount(vfs, "/sd")

# Create a file and write something to it
with open("/sd/test03.txt", "w") as file:
    file.write("Hello,WWWWORX22345 691!! SD World!\r\n")
    file.write("This is a test and seems to work@@022 ABCDEfg\r\n")

# Open the file we just created and read from it
with open("/sd/test03.txt", "r") as file:
    data = file.read()
    print(data)
#Now do DS18b20 temperature stuff. Thethird sensor.    
ds_pin = machine.Pin(22)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))

roms = ds_sensor.scan()
print('Found DS devices: ', roms)
while True:
  ds_sensor.convert_temp()
  time.sleep_ms(750)
  for rom in roms:
    print(rom)
    tempC = ds_sensor.read_temp(rom)
    tempF = tempC * (9/5) +32
    print('temperature (ºC):', "{:.2f}".format(tempC))
    print('temperature (ºF):', "{:.2f}".format(tempF))
    print()
  time.sleep(5)

When run, the screen looks like this. Note REPL output:

  
This board should be the basis of data collection from boardwalk garden project. Will need pi picoW and some network work to log and send the data.  

======================version 2 below================

Heading towards JLPCB board. PLus picoW AP network.

OLed first.





Comments

Popular posts from this blog

Reading LittleFS file into buffer for sending

ESP32 buttons and bouncing