Black Lives Matter - Action and Equality.
0

Continuously write ADXL345 values to SSD1306 OLED
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Continuously write ADXL345 values to SSD1306 OLED

by circuitsimon on Fri May 15, 2020 6:52 pm

Hi I have successfully written a piece of code that reads data from an ADXL345 accelerometer and outputs it to a SSD1306 OLED display.

Image

My question is how do I update the display with new data continuously, say every 0.5 second?
I am unable to find any tutorial on how to do this (does it even exist?) and I can't make it work on my own.

Thanks for reading.

This is my code (about 60 lines with comments):
Code: Select all | TOGGLE FULL SIZE
import time # Used for adding delays (sleep) in the code
import board # Used for handling board I/O
import displayio # Used for enabling Display communication
import terminalio # Used for providing a font when writing text
import adafruit_adxl34x # Used for communicating with the ADXL345 accelerometer
import adafruit_displayio_ssd1306 # Display driver used for communicating (displayio) with the SSD1306 OLED display
from adafruit_display_text import label # Used for writing text on the SSD1306 OLED display

# Create an i2c interface variable.
i2c = board.I2C()
# Setup the adxl345 communication protocol
accelerometer = adafruit_adxl34x.ADXL345(i2c)

### Setup the SSD1306 OLED display. ###
# If the microprocessor is reset, the display pins are not automatically released, this makes them available for use again.
displayio.release_displays()
# Define display_bus enabling communication with the SSD1306 OLED display.
display_bus = displayio.I2CDisplay(i2c, device_address=0x3c)
# Initialize the SSD1306 OLED display.
oled = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)

### Writing to the display ###
# Create the main Group that will contain all the other Groups.
displayContainer = displayio.Group(max_size=10)
oled.show(displayContainer)

# Define a Bitmap that stretches the entire width and some of the height of Display.
title_bitmap = displayio.Bitmap(128, 28, 1)
# Create a Palette and set color to white. Bitmap can handle 256 colors.
title_palette = displayio.Palette(1)
title_palette[0] = 0xFFFFFF
# Create a TileGrid from the Bitmap and Palette. Draw it using displayio.
title_background = displayio.TileGrid(title_bitmap, pixel_shader=title_palette, x=0, y=0)
# Add the TileGrid to the displayContainer Group
displayContainer.append(title_background)

# Create a Label with a string of text (no scaling) and add it to the displayContainer Group.
# Labels are centered vertically
title_text = "ADXL345 readings:"
title_label = label.Label(terminalio.FONT, text=title_text, color=0x000000, x=14, y=13)
displayContainer.append(title_label)

# Read ADXL345 values and add them to the displayContainer Group
xAxis_text = "X:" + str(round(accelerometer.acceleration[0], 1))
xAxis_label = label.Label(terminalio.FONT, text=xAxis_text, color=0xFFFFFF, x=10, y=40)
displayContainer.append(xAxis_label)

yAxis_text = "Y:" + str(round(accelerometer.acceleration[1], 1))
yAxis_label = label.Label(terminalio.FONT, text=yAxis_text, color=0xFFFFFF, x=53, y=40)
displayContainer.append(yAxis_label)

zAxis_text = "Z:" + str(round(accelerometer.acceleration[2], 1))
zAxis_label = label.Label(terminalio.FONT, text=zAxis_text, color=0xFFFFFF, x=90, y=40)
displayContainer.append(zAxis_label)

while True:
    pass

circuitsimon
 
Posts: 4
Joined: Fri May 15, 2020 6:30 pm

Re: Continuously write ADXL345 values to SSD1306 OLED

by circuitsimon on Sat May 16, 2020 9:09 am

I solved it! A good nights sleep was all I needed. I had spent most of the day wiring everything up and reading documentation on how to use circuitpython and the libraries needed. Today I realised I should just throw some python at the issue. After trying that and failing I went back to reading documentation, lmao! Pretty obvious but still satisfying considering I'm a uber noob at python. :)

The code still needs some polish but basically I combined the 3 seperate readings into one, then I add it as a Label and remove it again. Repeat. Only thing left to do is make the readings fit on the OLED display(too many digits) but that is easy.

old:
Code: Select all | TOGGLE FULL SIZE
# Read ADXL345 values and add them to the displayContainer Group
xAxis_text = "X:" + str(round(accelerometer.acceleration[0], 1))
xAxis_label = label.Label(terminalio.FONT, text=xAxis_text, color=0xFFFFFF, x=10, y=40)
displayContainer.append(xAxis_label)

yAxis_text = "Y:" + str(round(accelerometer.acceleration[1], 1))
yAxis_label = label.Label(terminalio.FONT, text=yAxis_text, color=0xFFFFFF, x=53, y=40)
displayContainer.append(yAxis_label)

zAxis_text = "Z:" + str(round(accelerometer.acceleration[2], 1))
zAxis_label = label.Label(terminalio.FONT, text=zAxis_text, color=0xFFFFFF, x=90, y=40)
displayContainer.append(zAxis_label)

while True:
    pass

new:
Code: Select all | TOGGLE FULL SIZE
# Read ADXL345 values and add them to the displayContainer Group
while True:
    allAxis = str(accelerometer.acceleration)
    allAxis_label = label.Label(terminalio.FONT, text=allAxis, color=0xFFFFFF, x=10, y=40)
    displayContainer.append(allAxis_label)
    time.sleep(0.3)
    displayContainer.remove(allAxis_label)

circuitsimon
 
Posts: 4
Joined: Fri May 15, 2020 6:30 pm

Re: Continuously write ADXL345 values to SSD1306 OLED

by circuitsimon on Sat May 16, 2020 10:32 am

Image
For anyone interested this is the final code.

Dependencies:
adafruit_bus_device
adafruit_display_text
adafruit_adxl34x.mpy
adafruit_displayio_ssd1306.mpy

Hardware:
Everything is connected through 3.3v, GND, SDA, SCL.
ADXL345
SSD1306 OLED (128x64)
ItsyBitsy M4 Express (or any CircuitPython compatible board)

Code:
Code: Select all | TOGGLE FULL SIZE
import time # Used for adding delays (sleep) in the code
import board # Used for handling board I/O
import displayio # Used for enabling Display communication
import terminalio # Used for providing a font when writing text
import adafruit_adxl34x # Used for communicating with the ADXL345 accelerometer
import adafruit_displayio_ssd1306 # Display driver used for communicating (displayio) with the SSD1306 OLED display
from adafruit_display_text import label # Used for writing text on the SSD1306 OLED display

# Create an i2c interface variable.
i2c = board.I2C()
# Setup the adxl345 communication protocol
accelerometer = adafruit_adxl34x.ADXL345(i2c)

### Setup the SSD1306 OLED display. ###
# If the microprocessor is reset, the display pins are not automatically released, this makes them available for use again.
displayio.release_displays()
# Define display_bus enabling communication with the SSD1306 OLED display.
display_bus = displayio.I2CDisplay(i2c, device_address=0x3c)
# Initialize the SSD1306 OLED display.
oled = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)

### Writing to the display ###
# Create the main Group that will contain all the other Groups.
displayContainer = displayio.Group(max_size=20)
oled.show(displayContainer)

# Define a Bitmap that stretches the entire width and some of the height of Display.
title_bitmap = displayio.Bitmap(64, 64, 1)
# Create a Palette and set color to white. Bitmap can handle 256 colors.
title_palette = displayio.Palette(1)
title_palette[0] = 0xFFFFFF
# Create a TileGrid from the Bitmap and Palette. Draw it using displayio.
title_background = displayio.TileGrid(title_bitmap, pixel_shader=title_palette, x=0, y=0)
# Add the TileGrid to the displayContainer Group
displayContainer.append(title_background)

# Create a Label with a string of text (no scaling) and add it to the displayContainer Group.
# Labels are centered vertically
title_text = "ADXL345\nreadings:"
title_label = label.Label(terminalio.FONT, text=title_text, color=0x000000, x=5, y=30)
displayContainer.append(title_label)

# Read ADXL345 values and add them to the displayContainer Group
while True:
    # Clean up and format the reading
    allAxis = ""
    x = ("x: ","y: ","z: ")
    zipped = zip(x,accelerometer.acceleration)
    for item in zipped:
        a = item[0] + str(round(item[1],1))
        allAxis += a + "\n"
    # Create Label and append/remove
    allAxis_label = label.Label(terminalio.FONT, text=allAxis, color=0xFFFFFF, x=70, y=40)
    displayContainer.append(allAxis_label)
    time.sleep(0.3)
    displayContainer.remove(allAxis_label)

circuitsimon
 
Posts: 4
Joined: Fri May 15, 2020 6:30 pm

Re: Continuously write ADXL345 values to SSD1306 OLED

by tannewt on Sun May 17, 2020 11:30 pm

Glad you figured it out. Nice job! Thanks for posting your final version.

tannewt
 
Posts: 1644
Joined: Thu Oct 06, 2016 8:48 pm

Re: Continuously write ADXL345 values to SSD1306 OLED

by circuitsimon on Mon May 18, 2020 7:05 pm

Thanks! I figured it would be good to post the progress for people like me. New to python! And thank you for all the work you put into this.

circuitsimon
 
Posts: 4
Joined: Fri May 15, 2020 6:30 pm

Please be positive and constructive with your questions and comments.