diff --git a/code/Blink.py b/code/Blink.py new file mode 100644 index 0000000000000000000000000000000000000000..21a38c63b23864c0123c3268cb9537a05d53d31f --- /dev/null +++ b/code/Blink.py @@ -0,0 +1,9 @@ +import machine +import time + +ledpin = machine.Pin(6, machine.Pin.OUT) + +while True: + ledpin.toggle() + time.sleep(0.5) + diff --git a/code/Blink02.py b/code/Blink02.py new file mode 100644 index 0000000000000000000000000000000000000000..688a0f941b6124e0e8bcd300424e473f28cf0bb5 --- /dev/null +++ b/code/Blink02.py @@ -0,0 +1,11 @@ +from machine import Pin +import time + +r_led = Pin(7, Pin.OUT) + +interval = 0.5 +while True: + r_led(1) + time.sleep(interval) + r_led(0) + time.sleep(interval) \ No newline at end of file diff --git a/code/PWM_potentiometer.py b/code/PWM_potentiometer.py new file mode 100644 index 0000000000000000000000000000000000000000..6ea6ff8fb9cefb0bfd799a9cf3da7537b007cbcc --- /dev/null +++ b/code/PWM_potentiometer.py @@ -0,0 +1,16 @@ +from machine import Pin, PWM, ADC +import time + +pin6 = Pin(6, Pin.OUT, value=0) +pin7 = Pin(7, Pin.OUT, value=0) + +adc = ADC(Pin(26)) # create ADC object on ADC pin +pwm0 = PWM(Pin(0)) # create PWM object from a pin +pwm0.freq(1000) # set frequency + + +while True: + value = adc.read_u16() # read value, 0-65535 across voltage range 0.0v - 3.3v + print(value) + pwm0.duty_u16(value) # set duty cycle, range 0-65535 + time.sleep_ms(100) \ No newline at end of file diff --git a/code/SPI_23K256.py b/code/SPI_23K256.py new file mode 100644 index 0000000000000000000000000000000000000000..7e31dfdc13a3b832d968abff23b6b4987a1cd2ba --- /dev/null +++ b/code/SPI_23K256.py @@ -0,0 +1,57 @@ +import machine +import time + +############################################################################### +# Constants + +READ = 0x03 +WRITE = 0x02 +READ_S_R = 0x05 +WRITE_S_R = 0x01 + +SEQ_RW = 0x01 << 6 #B6 is 1 +BYTE_RW = 0x00 << 6 #zeros +PAGE_RW = 0x02 << 6 #B7 is 1 + + +######################################################## + +# Assign chip select (CS) pin +cs = machine.Pin(1, machine.Pin.OUT) +time.sleep(0.1) + + +# Initialize SPI. Use SPI0 with pins 1,2,3,4. +spi = machine.SPI(0, + baudrate=1000000, + polarity=0, + phase=1, + bits=8, + firstbit=machine.SPI.MSB, + sck=machine.Pin(2), + mosi=machine.Pin(3), + miso=machine.Pin(4)) + + +#write to status register +cs.value(1) + +msg = bytearray() +msg.append(WRITE_S_R) +msg.append(SEQ_RW) +print(msg) +cs.value(0) +spi.write(msg) +cs.value(1) + + +#read status register +msg = bytearray() +msg.append(READ_S_R) + +cs.value(0) +spi.write(msg) +data = spi.read(1) +cs.value(1) +print(data) + diff --git a/code/SPI_axdl343_RGB.py b/code/SPI_axdl343_RGB.py new file mode 100644 index 0000000000000000000000000000000000000000..d4e621cc15320eaeec5d8d83f21eb5f9763d3e11 --- /dev/null +++ b/code/SPI_axdl343_RGB.py @@ -0,0 +1,153 @@ +import machine +from machine import Pin, PWM +import utime +import ustruct +import sys + +############################################################################### +# Constants + +# Registers +REG_DEVID = 0x00 +REG_POWER_CTL = 0x2D +REG_DATAX0 = 0x32 + + +# Other constants +DEVID = 0xE5 +SENSITIVITY_2G = 1.0 / 256 # (g/LSB) +EARTH_GRAVITY = 9.80665 # Earth's gravity in [m/s^2] + +############################################################################### +# Settings + +#PWM pins for RGB +# r_led = Pin(6, Pin.OUT) +# r_led(0) +pwm_r = PWM(Pin(6)) # create PWM object from a pin +pwm_g = PWM(Pin(7)) # create PWM object from a pin +pwm_b = PWM(Pin(0)) # create PWM object from a pin +pwm_r.freq(1000) # set frequency +pwm_g.freq(1000) # set frequency +pwm_b.freq(1000) # set frequency +#pwm_r.duty_u16(40000) # set duty cycle, range 0-65535 + +# Assign chip select (CS) pin (and start it high) +cs = machine.Pin(1, machine.Pin.OUT) + +# Initialize SPI. Follow pinout for xiao RP2040. Exposed pins are as follows and use SPI0. +spi = machine.SPI(0, + baudrate=1000000, + polarity=1, + phase=1, + bits=8, + firstbit=machine.SPI.MSB, + sck=machine.Pin(2), + mosi=machine.Pin(3), + miso=machine.Pin(4)) + + +############################################################################### +# Functions + +def reg_write(spi, cs, reg, data): + """ + Write 1 byte to the specified register. + """ + + # Construct message (set ~W bit low, MB bit low) + msg = bytearray() + msg.append(0x00 | reg) + msg.append(data) + + # Send out SPI message + cs.value(0) + spi.write(msg) + cs.value(1) + + +def reg_read(spi, cs, reg, nbytes=1): + """ + Read byte(s) from specified register. If nbytes > 1, read from consecutive + registers. + """ + + # Determine if multiple byte (MB) bit should be set + if nbytes < 1: + return bytearray() + elif nbytes == 1: + mb = 0 + else: + mb = 1 + + # Construct message (set ~W bit high) + msg = bytearray() + msg.append(0x80 | (mb << 6) | reg) + + # Send out SPI message and read + cs.value(0) + spi.write(msg) + data = spi.read(nbytes) + cs.value(1) + + return data + + +############################################################################### +# Main + +# Start CS pin high +cs.value(1) + +# Workaround: perform throw-away read to make SCK idle high +reg_read(spi, cs, REG_DEVID) + +# Read device ID to make sure that we can communicate with the ADXL343 +data = reg_read(spi, cs, REG_DEVID) +if (data != bytearray((DEVID,))): + print("ERROR: Could not communicate with ADXL343") + sys.exit() + +# Read Power Control register +data = reg_read(spi, cs, REG_POWER_CTL) +print(data) + +# Tell ADXL343 to start taking measurements by setting Measure bit to high +data = int.from_bytes(data, "big") | (1 << 3) +reg_write(spi, cs, REG_POWER_CTL, data) + +# Test: read Power Control register back to make sure Measure bit was set +data = reg_read(spi, cs, REG_POWER_CTL) +print(data) + +# Wait before taking measurements +utime.sleep(2.0) + +# Run forever +while True: + # Read X, Y, and Z values from registers (16 bits each) + data = reg_read(spi, cs, REG_DATAX0, 6) + + # Convert 2 bytes (little-endian) into 16-bit integer (signed) + acc_x = ustruct.unpack_from("<h", data, 0)[0] + acc_y = ustruct.unpack_from("<h", data, 2)[0] + acc_z = ustruct.unpack_from("<h", data, 4)[0] + + # Convert to PWM duty cycles + #print(acc_x) + pwm_r.duty_u16(acc_x*acc_x) # set duty cycle, range 0-65535 + pwm_g.duty_u16(acc_y*acc_y) # set duty cycle, range 0-65535 + pwm_b.duty_u16(acc_z*acc_z) # set duty cycle, range 0-65535 + #print(pwm_r.duty_u16()) + # Convert measurements to [m/s^2] + acc_x = acc_x * SENSITIVITY_2G * EARTH_GRAVITY + acc_y = acc_y * SENSITIVITY_2G * EARTH_GRAVITY + acc_z = acc_z * SENSITIVITY_2G * EARTH_GRAVITY + + # Print results + print("X:", "{:.2f}".format(acc_x), \ + "| Y:", "{:.2f}".format(acc_y), \ + "| Z:", "{:.2f}".format(acc_z)) + print(pwm_r.duty_u16()) + + utime.sleep(0.1) \ No newline at end of file diff --git a/code/SPI_axdl343_tests.py b/code/SPI_axdl343_tests.py new file mode 100644 index 0000000000000000000000000000000000000000..f6c70eae23b232f091f385d12d78fbe554f5ac25 --- /dev/null +++ b/code/SPI_axdl343_tests.py @@ -0,0 +1,164 @@ +import machine +from machine import Pin, PWM +import utime +import ustruct +import sys + +############################################################################### +# Constants + +# Registers +REG_DEVID = 0x00 +REG_POWER_CTL = 0x2D +REG_DATAX0 = 0x32 + + +# Other constants +DEVID = 0xE5 +SENSITIVITY_2G = 1.0 / 256 # (g/LSB) +EARTH_GRAVITY = 9.80665 # Earth's gravity in [m/s^2] + +############################################################################### +# Settings + +#PWM pins for RGB +# r_led = Pin(6, Pin.OUT) +# r_led(0) +pwm_r = PWM(Pin(6)) # create PWM object from a pin +pwm_g = PWM(Pin(7)) # create PWM object from a pin +pwm_b = PWM(Pin(0)) # create PWM object from a pin +pwm_r.freq(1000) # set frequency +pwm_g.freq(1000) # set frequency +pwm_b.freq(1000) # set frequency +#pwm_r.duty_u16(40000) # set duty cycle, range 0-65535 + +# Assign chip select (CS) pin (and start it high) +cs = machine.Pin(1, machine.Pin.OUT) + +# Initialize SPI. Follow pinout for xiao RP2040. Exposed pins are as follows and use SPI0. +spi = machine.SPI(0, + baudrate=1000000, + polarity=1, + phase=1, + bits=8, + firstbit=machine.SPI.MSB, + sck=machine.Pin(2), + mosi=machine.Pin(3), + miso=machine.Pin(4)) + + +############################################################################### +# Functions + +def reg_write(spi, cs, reg, data): + """ + Write 1 byte to the specified register. + """ + + # Construct message (set ~W bit low, MB bit low) + msg = bytearray() + msg.append(0x00 | reg) + msg.append(data) + + # Send out SPI message + cs.value(0) + spi.write(msg) + cs.value(1) + + +def reg_read(spi, cs, reg, nbytes=1): + """ + Read byte(s) from specified register. If nbytes > 1, read from consecutive + registers. + """ + + # Determine if multiple byte (MB) bit should be set + if nbytes < 1: + return bytearray() + elif nbytes == 1: + mb = 0 + else: + mb = 1 + + # Construct message ( for n>1, set MB bit (6) high) see p13 of datasheet. + msg = bytearray() + msg.append(0x80 | (mb << 6) | reg) + + # Send out SPI message and read + cs.value(0) + spi.write(msg) + data = spi.read(nbytes) + cs.value(1) + + return data + + +############################################################################### +# Main + +# Start CS pin high +cs.value(1) + +# Workaround: perform throw-away read to make SCK idle high +reg_read(spi, cs, REG_DEVID) + +# Read device ID to make sure that we can communicate with the ADXL343 +data = reg_read(spi, cs, REG_DEVID) +if (data != bytearray((DEVID,))): + print("ERROR: Could not communicate with ADXL343") + sys.exit() + +# Read Power Control register +data = reg_read(spi, cs, REG_POWER_CTL) +print(data) + +# Tell ADXL343 to start taking measurements by setting Measure bit to high +data = int.from_bytes(data, "big") | (1 << 3) +reg_write(spi, cs, REG_POWER_CTL, data) + +# Test: read Power Control register back to make sure Measure bit was set +data = reg_read(spi, cs, REG_POWER_CTL) +print(data) + +#The following test lines read an arbitrary register: + +cs.value(0) +msg = bytearray() +msg.append(0x80 | 0x2c) # +spi.write(msg) +print(msg) +data = spi.read(1) #then immediately read. +cs.value(1) #terminate the read. +print(data) + +# Wait before taking measurements +#utime.sleep(2.0) + +# Run forever +# while True: +# # Read X, Y, and Z values from registers (16 bits each) +# data = reg_read(spi, cs, REG_DATAX0, 6) +# +# # Convert 2 bytes (little-endian) into 16-bit integer (signed) +# acc_x = ustruct.unpack_from("<h", data, 0)[0] +# acc_y = ustruct.unpack_from("<h", data, 2)[0] +# acc_z = ustruct.unpack_from("<h", data, 4)[0] +# +# # Convert to PWM duty cycles +# #print(acc_x) +# pwm_r.duty_u16(acc_x*acc_x) # set duty cycle, range 0-65535 +# pwm_g.duty_u16(acc_y*acc_y) # set duty cycle, range 0-65535 +# pwm_b.duty_u16(acc_z*acc_z) # set duty cycle, range 0-65535 +# #print(pwm_r.duty_u16()) +# # Convert measurements to [m/s^2] +# acc_x = acc_x * SENSITIVITY_2G * EARTH_GRAVITY +# acc_y = acc_y * SENSITIVITY_2G * EARTH_GRAVITY +# acc_z = acc_z * SENSITIVITY_2G * EARTH_GRAVITY +# +# # Print results +# print("X:", "{:.2f}".format(acc_x), \ +# "| Y:", "{:.2f}".format(acc_y), \ +# "| Z:", "{:.2f}".format(acc_z)) +# print(pwm_r.duty_u16()) +# +# utime.sleep(0.1) \ No newline at end of file diff --git a/img/adxl343.jpg b/img/adxl343.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a3630e8749033cbf6ff7e504846c44bcaee88811 Binary files /dev/null and b/img/adxl343.jpg differ