prjbrook

boardwalk_oled_SD1.py DS18b20 SD Oled

Sep 20th, 2024
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. #boardwalkOledSD0.py.
  2. print("Simple SD card with OLED") #Sat Sep 21 12:13:19 NZST 2024
  3. import machine
  4. import sdcard
  5. import os
  6. import onewire, ds18x20, time
  7.  
  8. #Next lines are for SD card. Originally from digikey
  9. from machine import Pin, I2C
  10. from ssd1306 import SSD1306_I2C    #make sure ssd1306 for micropythin is in libd
  11. i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
  12. oled = SSD1306_I2C(128, 64, i2c)
  13.  
  14. oled.text("MingBdy Hardw7Oct", 0, 0)
  15. oled.show()  
  16.  
  17.  
  18. # Assign chip select (CS) pin (and start it high)  
  19. cs = machine.Pin(9, machine.Pin.OUT)  #does this really start high?
  20.  
  21. # Intialize SPI peripheral (start with 1 MHz) Check it IS 1000000
  22. spi = machine.SPI(1,
  23.                   baudrate=100000,
  24.                   polarity=0,
  25.                   phase=0,
  26.                   bits=8,
  27.                   firstbit=machine.SPI.MSB,
  28.                   sck=machine.Pin(10),
  29.                   mosi=machine.Pin(11),
  30.                   miso=machine.Pin(8))
  31.  
  32. # Initialize SD card
  33. sd = sdcard.SDCard(spi, cs)     #make sure sdcard.py in path
  34.  
  35. vfs = os.VfsFat(sd)
  36. os.mount(vfs, "/sd")
  37.  
  38. # Create a file and write something to it
  39. with open("/sd/test03.txt", "w") as file:
  40.     file.write("Hello,WWWWORX22345 69## SD World!\r\n")
  41.     file.write("This is a test and seems to work@@022 ABCDEfg1 Sat Sep 21 12:09:51 NZST 2024\r\n")
  42.     file.write("Fri Sep 20 12:10:40 NZST 2024\r\n")
  43. # Open the file we just created and read from it
  44. with open("/sd/test03.txt", "r") as file:
  45.     data = file.read()
  46.     print(data)
  47.    
  48. #Now do DS18b20 temperature stuff. Thethird sensor.    
  49. ds_pin = machine.Pin(22)
  50. ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
  51.  
  52. roms = ds_sensor.scan()
  53. print('Found DS devices: ', roms)
  54. while True:
  55.   ds_sensor.convert_temp()
  56.   time.sleep_ms(750)
  57.   for rom in roms:
  58.     print(rom)
  59.     tempC = ds_sensor.read_temp(rom)
  60.     tempF = tempC * (9/5) +32
  61.     print('temperature (ºC):', "{:.2f}".format(tempC))
  62.     print('temperature (ºF):', "{:.2f}".format(tempF))
  63.     print()
  64.   time.sleep(5)
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment