prjbrook

ThreeSensors2.py Micropython

Jul 31st, 2024
248
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. #all three sensors, OLED, SD, DS18b20 Thu Aug  1 11:24:10 NZST 2024 Worked.
  2. #Combined digikey4 with Tom0Oled to get CD card reader and OLED display in one program. Worked 4:28,30/07/24
  3. #first, do imports for SD card
  4. print("Simple SD card testDigiKey1.1 ")  #Tue Jun 28 17:09:24 NZST 2022
  5. print("Pi Pico and Code with Thonny.")
  6. import machine
  7. import sdcard
  8. import os  #why not uos?
  9. import onewire, ds18x20, time
  10.  
  11.  
  12. #Next lines are for CD card. Originally from digikey4
  13. from machine import Pin, I2C #12:04, 30/07/24 Worked
  14. from ssd1306 import SSD1306_I2C
  15.  
  16. i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
  17. oled = SSD1306_I2C(128, 64, i2c)
  18.  
  19. oled.text("Tom's Hardware69", 0, 0)
  20. oled.show()  
  21.  
  22.  
  23. # Assign chip select (CS) pin (and start it high)  
  24. cs = machine.Pin(9, machine.Pin.OUT)  #does this really start high?
  25.  
  26. # Intialize SPI peripheral (start with 1 MHz) Check it IS 1000000
  27. spi = machine.SPI(1,
  28.                   baudrate=100000,
  29.                   polarity=0,
  30.                   phase=0,
  31.                   bits=8,
  32.                   firstbit=machine.SPI.MSB,
  33.                   sck=machine.Pin(10),
  34.                   mosi=machine.Pin(11),
  35.                   miso=machine.Pin(8))
  36.  
  37. # Initialize SD card
  38. sd = sdcard.SDCard(spi, cs)
  39.  
  40. vfs = os.VfsFat(sd)
  41. os.mount(vfs, "/sd")
  42.  
  43. # Create a file and write something to it
  44. with open("/sd/test03.txt", "w") as file:
  45.     file.write("Hello,WWWWORX22345 691!! SD World!\r\n")
  46.     file.write("This is a test and seems to work@@022 ABCDEfg\r\n")
  47.  
  48. # Open the file we just created and read from it
  49. with open("/sd/test03.txt", "r") as file:
  50.     data = file.read()
  51.     print(data)
  52. #Now do DS18b20 temperature stuff. Thethird sensor.    
  53. ds_pin = machine.Pin(22)
  54. ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
  55.  
  56. roms = ds_sensor.scan()
  57. print('Found DS devices: ', roms)
  58. while True:
  59.   ds_sensor.convert_temp()
  60.   time.sleep_ms(750)
  61.   for rom in roms:
  62.     print(rom)
  63.     tempC = ds_sensor.read_temp(rom)
  64.     tempF = tempC * (9/5) +32
  65.     print('temperature (ºC):', "{:.2f}".format(tempC))
  66.     print('temperature (ºF):', "{:.2f}".format(tempF))
  67.     print()
  68.   time.sleep(5)
  69.  
  70.  
  71.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment