Skip to content

Instantly share code, notes, and snippets.

@sebi5361
Last active October 9, 2019 18:37
Show Gist options
  • Save sebi5361/94d0fd379196240f917e19aeb71c56e5 to your computer and use it in GitHub Desktop.
Save sebi5361/94d0fd379196240f917e19aeb71c56e5 to your computer and use it in GitHub Desktop.
# 2019-09-26 SK, MIT license
"""
MicroPython code - nRF port
Accessing the peripherals of the MPow/Desay DS-D6 wristband
"""
"""
pins.cvs content:
USB_VOLT,P2
BAT_VOLT,P3
OLED_RES,P4
OLED_SCK,P5
OLED_MOSI,P6
HEART_SCL,P7
HEART_SDA,P8
ACC_SCL,P13
ACC_SDA,P14
ACC_INT,P15
UART_RX,P22
UART_TX,P23
MOTOR,P25
HEART_EN,P26
OLED_DC,P28
OLED_CS,P29
SWITCH,P30
"""
## Motor
from machine import Pin
from time import sleep_ms
motor = Pin("MOTOR", Pin.OUT)
motor.on()
sleep_ms(1000)
motor.off()
# PWM TEST. Doesn't work
# from machine import Pin, PWM
# motor = Pin("MOTOR", Pin.OUT)
# motor_pwm = PWM(motor, period = 16000, duty=16000)
# motor_pwm.deinit()
## Switch
from time import sleep_ms
from machine import Pin
switch = Pin("SWITCH", Pin.IN)
count = 0
while count<100:
count += 1
print(switch.value())
sleep_ms(100)
## Voltages
from time import sleep_ms
from machine import Pin, ADC
usb_volt_pin = Pin("USB_VOLT", Pin.IN)
usb_volt_adc = ADC(usb_volt_pin)
bat_volt_pin = Pin("BAT_VOLT", Pin.IN)
bat_volt_adc = ADC(bat_volt_pin)
inc2volt = 5/200 # 5 volts for 200 increments
count = 0
while count<100:
count += 1
print("USB voltage : {}V".format(inc2volt * usb_volt_adc.value()))
print("Bat. voltage: {}V".format(inc2volt * bat_volt_adc.value()))
sleep_ms(100)
## Screen SSD1306 128*32 OLED display with 64 line memory (requires slight modification of the standard library)
from time import sleep_ms
from machine import Pin, SPI
from ssd1306dsd6 import SSD1306_SPI
oled_sck = Pin("OLED_SCK")
oled_mosi = Pin("OLED_MOSI")
oled_spi = SPI("OLED_SPI", sck=oled_sck, mosi=oled_mosi) # OLED is on SPI bus 1. sck and mosi can be omitted as they are implicit.
oled_dc = Pin("OLED_DC")
oled_res = Pin("OLED_RES")
oled_cs = Pin("OLED_CS")
oled = SSD1306_SPI(width=128, height=32, spi=oled_spi, dc=oled_dc, res=oled_res, cs=oled_cs)
oled.poweron()
oled.write_cmd(0xc0) # To mirror screen
oled.fill(0)
oled.text('Hello', 0, 0)
oled.text('World', 0, 10)
oled.show()
sleep_ms(5000)
oled.poweroff()
## Heart Rate Sensor Pixart PAH8001EI-2G
from machine import Pin, I2C
from ustruct import unpack_from
heart_scl = Pin("HEART_SCL")
heart_sda = Pin("HEART_SDA")
heart_en = Pin("HEART_EN", Pin.OUT)
heart_en.on() # Turn ON heart rate sensor (i2c gets feedback + green LED shines)
heart_i2c = I2C(0, scl=heart_scl, sda=heart_sda) # Heart rate sensor is on bus 0. scl and sda should be implicit. However omitting them yields an error.
i2c_bus0_devices = heart_i2c.scan()
print(i2c_bus0_devices) # Heart rate sensor address is 107
heart_i2c_address = 107
heart_i2c.writeto_mem(heart_i2c_address, 0x7f, 0x00) # Change to bank 0
heart_prod_id1 = heart_i2c.readfrom_mem(heart_i2c_address, 0x00, 1) # Product ID1
unpack_from("B", heart_prod_id1)[0] # 0x30=48
heart_prod_id2 = heart_i2c.readfrom_mem(heart_i2c_address, 0x01, 1) # Product ID2
hex(unpack_from("B", heart_prod_id2)[0]) # 0xdx
heart_en.off()
## Accelerometer Kionix KX023
from machine import Pin, I2C
from array import array
from time import sleep_ms
acc_scl = Pin("ACC_SCL")
acc_sda = Pin("ACC_SDA")
acc_int = Pin("ACC_INT", Pin.OUT)
acc_i2c = I2C(1, scl = acc_scl, sda = acc_sda) # Accelerometer is on bus 1. scl and sda should be implicit. However omitting them yields an error.
i2c_bus1_devices = acc_i2c.scan()
print(i2c_bus1_devices) # Acc i2c address is 0x1f=31
acc_i2c_address = 31
acc_whoami_address = 0x0f
acc_buffer1 = array("B", [0]) # uint
acc_i2c.readfrom_mem_into(acc_i2c_address, acc_whoami_address, acc_buffer1)
acc_whoami_content = acc_buffer1[0]
print(acc_whoami_content) # Must be 0x15=21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment