Guest

Untitled 1058

Apr 5th, 2026
11
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
None 2.94 KB | None | 0 0
  1. import smbus2
  2. import time
  3.  
  4. # MAX30102 I2C address
  5. MAX30102_ADDR = 0x57
  6.  
  7. # Registers
  8. REG_INTR_STATUS_1 = 0x00
  9. REG_FIFO_DATA = 0x07
  10. REG_MODE_CONFIG = 0x09
  11. REG_SPO2_CONFIG = 0x0A
  12. REG_LED1_PA = 0x0C
  13. REG_LED2_PA = 0x0D
  14.  
  15. bus = smbus2.SMBus(1) # RPi I2C bus 1
  16.  
  17. # Reset and Setup
  18. bus.write_byte_data(MAX30102_ADDR, REG_MODE_CONFIG, 0x40) # Reset
  19. time.sleep(0.1)
  20. bus.write_byte_data(MAX30102_ADDR, REG_MODE_CONFIG, 0x03) # Heart Rate + SpO2 mode
  21. bus.write_byte_data(MAX30102_ADDR, REG_SPO2_CONFIG, 0x27) # 411us pulse, 100 samples/sec
  22. bus.write_byte_data(MAX30102_ADDR, REG_LED1_PA, 0x24) # Red LED current
  23. bus.write_byte_data(MAX30102_ADDR, REG_LED2_PA, 0x24) # IR LED current
  24.  
  25. print("Reading Raw Data (Red/IR)... Press Ctrl+C to stop")
  26.  
  27. try:
  28. while True:
  29. # Read 6 bytes from FIFO (3 for Red, 3 for IR)
  30. data = bus.read_i2c_block_data(MAX30102_ADDR, REG_FIFO_DATA, 6)
  31. red = (data[0] << 16 | data[1] << 8 | data[2]) & 0x3FFFF
  32. ir = (data[3] << 16 | data[4] << 8 | data[5]) & 0x3FFFF
  33.  
  34. print(f"RED: {red}, IR: {ir}")
  35. time.sleep(0.05)
  36. except Exception as e:
  37. print(f"Error: {e}")
  38. finally:
  39. bus.close()
RAW Paste Data Copied