Tilt Sensing with the OLED and BCore


Overview: 

The OLED interface board has an accelerometer onboard, which allows for tilt angle sensing. The accelerometer is a 3-axis type from Analog Devices (ADXL335).  Getting values form the tilt sensor on the OLED board is extremely easy. 

 

What you will need:

Hardware

 

Software


Hardware Setup

Make sure that the BCore100 Board is connected to the OLED Board. Refer to this tutorial for more detail.

 


Getting the Values

 

Before you read the values of the tilt sensor, you should initialize the OLED module. You can read the sensor values as such


SX = OLED.Accel.X()
SY = OLED.Accel.Y()
SZ = OLED.Accel.Z()


where SX, SY and SZ are all integers. 

OLED.Accel.X() returns the x-axis value, 
OLED.Accel.Y() returns the y-axis value and 
OLED.Accel.Z() returns the z-axis value.


You might want the try the following code which returns and displays the values of the sensor in the Sonata debug window.


Dim SX, SY, SZ As Integer

'============================================================================
Public Sub Main()
DEBUG.PRINT "TILT SENSING"
High(56)
'INIT THE OLED
OLED.Init(BCK.COLOUR.BLACK)
OLED.Background.COLOUR = BCK.COLOUR.BLACK
OLED.Foreground.COLOUR = BCK.Colour.White
OLED.Orientation = Const.OLED.Landscape

Do
SX = OLED.Accel.X()
SY = OLED.Accel.Y()
SZ = OLED.Accel.Z()
Debug.Print Cstr(SX);" ";CSTR(SY);" ";CSTR(SZ)

Delay(50)
Loop

End Sub
'============================================================================

Tilt values illustrated

The program above will generate an output in the debug window that looks like this:

TILT SENSING
525 524 613
517 520 618
517 524 618
516 520 614
525 521 612
.....


ADXL335 Flat Values


These values are when the OLED module is flat on the table. A typical value for the x-axis when the OLED module is flat on the table is around 520. The y-axis also will give you around 520. The z-axis value is around 620.


The values changes when you change the position of the OLED board. (Note: X/Y/Z Axes correspond to the rectangular shape of the 3d drawings)

ADXL335 Accelerometer X Axis Tilt


For example, tilting the OLED board along the X-Axis will result in the above values.

 

ADXL335 Accelerometer Y Axis Tilt

A tilt in the Y-Axis results in a change in values along the Y-Axis, as illustrated above.

 

Plotting the values onto the OLED Screen

 

Accelerometer Graph OLED Screen

 

 

Getting values from the tilt sensor is fun, but if we can show the values on the OLED like an oscilloscope, that would give us a good visual representation on how the sensor is behaving.


To draw the tilt sensor values on the OLED would take a little bit of work. Essentially we are going to take the x-axis reading and plot it on the screen. The code is shown below. But you can also download the entire project in a zip package right at the bottom of this tutorial.

'CODE1
Dim SX, SY, SZ As Integer
Public Const MID_POS As Integer = 100
Public Const SX_MID As Integer = 520
Dim Pt(325) As point
DIM P1, P2, P3 as Point

'============================================================================
Public Sub Main()
Dim i, TVAL, X_POS, TEMP As Integer
DEBUG.PRINT "TILT SENSING"

'INIT THE OLED
High(56)
OLED.Init(BCK.COLOUR.BLACK)
OLED.Background.COLOUR = BCK.COLOUR.BLACK
OLED.Foreground.COLOUR = BCK.Colour.White
OLED.Orientation = Const.OLED.Landscape

'init variables
For i = 0 to 324
Pt(i) = [0,0]
Next

'draw white line
P1 = [0, MID_POS]
P2 = [319, MID_POS]
OLED.DRAW.LINE(P1,P2)

X_POS = 0
Do
'GET READING
SX = OLED.Accel.X()
SY = OLED.Accel.Y()
SZ = OLED.Accel.Z()
'Debug.Print Cstr(SX);" ";CSTR(SY);" ";CSTR(SZ)

TVAL = SX 'WE ARE ONLY INTERESTED IN X-AXIS

P1.X = X_POS
'DELETE PREVIOUS POINTS AND DRAW WHITE LINE
IF X_POS < 319 THEN
OLED.Foreground.COLOUR = BCK.Colour.BLACK
P3 = Pt(X_POS)
P2 = Pt(X_POS+1)
OLED.Draw.LINE(P3, P2)
Else
OLED.Draw.Point(Pt(X_POS), Const.Color.Black)
END If
P1.Y = MID_POS
OLED.Draw.Point(P1, Const.Color.White) 'DRAW THE MID LINE

'DRAW TVAL
P1.Y = MID_POS + ((TVAL - SX_MID))
Pt(X_POS) = P1
IF X_POS = 0 THEN
OLED.Draw.Point(P1, Const.Color.Blue)
Else
OLED.Foreground.COLOUR = BCK.Colour.BLUE
P2 = Pt(X_POS-1)
OLED.Draw.LINE(P2, P1)
END If

'CHECK IF WE HAVE COME TO THE END OF THE OLED SCREEN
X_POS = X_POS + 1
IF X_POS = 320 THEN
X_POS = 0
END If

Delay(5)
Loop

End Sub
'============================================================================

Tilt the OLED module in the x-axis and watch how the values response it. To get the value of the y-axis, just change the line,


TVAL = SX 'WE ARE ONLY INTERESTED IN X-AXIS

to

TVAL = SY	'WE ARE ONLY INTERESTED IN X-AXIS


The code above is obviously not complete. For example, the code does not take into consideration if the TVAL is out of range from the screen. (Thereby drawing a straight line from the top to the bottom of the screen)

Take note that large values of acceleration are involved when objects collide with each other (particularly rigid ones), and this is reflected in the value of the accelerometer reading TVAL when the board is struck by another object. See it for yourself! Just knock the board against the table, with hand wrapped around it to cushion the impact and avoid damaging the accelerometer.



For more information on tilt sensing, you might want to read up on the ADXL335 datasheet.
You can also check out an example code where you could choose which axis to display by selecting it on the OLED screen.

If you look at the plotted points, you will notice that the values are noisy. One way to overcome this is to filter the readings. Let’s do this in the next section of the tutorial. (Coming Soon) 

 


 

Download the entire project here