PiicoDev 3-Axis Accelerometer LIS3DH | Guide for Micro:Bit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
an accelerometer is a device that measures acceleration they're used in engineering to measure vibration in cars buildings or machines really high-end accelerometers are even used in navigation and most smartphones contain an accelerometer to keep the display upright when the screen is rotated the effects of gravity on an object are indistinguishable from acceleration which means that an accelerometer at rest on the surface of the earth will measure a total acceleration of about 9.8 meters per second squared upwards while an accelerometer in freefall will measure zero let's get started with the picodev three axis accelerometer we'll walk through some examples to read acceleration infer tilt angle using gravity and even detect tapping and shaking [Music] the picodev accelerometer is a three axis device that measures acceleration separately in three orthogonal axes the axes are labeled on the board as x y and z this is the end of the arrow that's pointing straight out of the board each arrow indicates the direction of positive acceleration there are two picadev connectors to allow daisy chain connections the address switch labelled asw allows you to select one of two addresses for this tutorial the asw switch should be in the lower off position for more experience makers there's also a solderable breakout that brings out two independent interrupts as well and if at any point in the tutorial you have any uncertainty or some questions you're not alone if you're in a school ask your teacher if you're in a maker space then ask the makerspace coordinator otherwise open a thread on our forums we're full-time makers and happy to help okay let's connect the accelerometer to a micro bit and get started with some examples plug a micro bit v2 into the picodev adapter for micro bit making sure the buttons are facing up connect a pikadev cable to one of the picodev connectors and connect the other end to a picodev three axis accelerometer again make sure the asw address switch is in the off position then connect your micro bit to a computer with a usb cable this first example reads the acceleration in three axes and prints the data in meters per second squared we'll be referring to the guide article a lot in this video if you don't already have it open find a link in the video description find the download section of the article and download the two files we'll need right click pikadev unified and save link as i'm going to save my files to a picodev directory in my documents and right click the device module and save link as open funny navigate to where you saved those files and upload both to your micro bit we know that successful if we see micro bit in the files pane and we have those two files uploaded if you need help getting started with funny for micro bit check the article for a deeper dive in how to get started with your first micropython project back in the article find the first example and copy the example code and i'll paste this into the untitled window in funny and save that to your micro bit as main.py click run and you ought to see some data streaming up the shell i have the plotter open here plotting that data if you don't see the plotter you can enable it from the view plotter menu and this is the acceleration data coming straight off the accelerometer we can see three lines one for each axis this red z-axis is at about 10 meters per second squared and that makes sense the z-axis is pointing straight up so it's seeing 1g or about 9.8 meters per second squared if i shake the accelerometer the data goes pretty crazy i'll slowly turn the accelerometer and we can see that plot slowly change so now we have the x-axis at about negative 10 meters per second and that makes sense because the positive x-direction is pointing down if i point the positive x-direction up it goes to 10 and for completeness i can point the y-axis up or roll the y-axis down if we take a look at that example code we start off with importing the device driver and a function to create a delay we call the accelerometer initialization function and that returns an accelerometer object that we call motion so every time we see motion in this script we're referring to the accelerometer we set the range property to 2. this is the full scale range that can be read on each axis in g so we're setting to 2g which is the most sensitive but you could also use 4 8 or 16 g in the infinite loop we read from the acceleration property which is a tuple for x y and z acceleration and so we assign values to three variables x y and z we round that data to two decimal places which makes this print look a bit nicer and then we just create a string which concatenates a label in this case x with the data the string of that data x then we do the same for y and the same for z finally we print the string and there's a short delay before returning to the top of the loop find the next example which is all about reading angle we can infer tilt angle from the acceleration data copy that example and paste it into main this example looks really similar to the first one we have the same initialization this time around we're reading the angle property and we're printing the angle about the y-axis run the script that will save to the micro bit and run and we can see a single angle parameter being printed now to figure out which angle the y-axis rotates about find the y-axis and point your right hand thumb in that direction the direction your fingers curl is the direction of positive angle so if i tilt the accelerometer in this direction the angle should increase and if i tilt it in the opposite direction it will decrease and we can actually tilt all the way around until we pass 180 degrees and go into the negative angles so this will measure between negative 180 and 180 degrees now if i try to run this script with the accelerometer sitting flat on the table and measure the angle about the z-axis we actually don't get meaningful data here because the two axes used to calculate that angle are not in the direction of gravity you can see i can slowly rotate this accelerometer around and i'm just getting nonsense data it's only when i lift the accelerometer up and get the x and y axes perpendicular to gravity that i can create meaningful data it's impossible to calculate a teal angle about this axis because we don't have any information to measure find the next example for detecting tapping and copy that into main after the normal setup we call the set tap method the first argument is a 1 because we want to detect a single tap and there's also a named parameter called threshold which we're setting to 40. you can think of this as how strong the tapping needs to be then in the infinite loop we just query if motion.tapped is true and if it is we print a one otherwise we print a zero and so by using that data in the plot we should be able to plot tap events run the script and we are printing all zeros which means that motion dot tapped is false but if i knock the desk nearby the sensor then we get a one and i have to knock quite firmly for that to work if i set this to something really low then i don't have to tap nearly as hard to register a tap event now it's possible to configure the accelerometer to only detect double taps instead set the first argument to a 2 to indicate double taps and we'll extend this loop time so that we don't miss any double taps rerun the script and now if i knock once we get no tap it's only when i double knock that we get that spike in the graph showing that we had a double tap event i'll comment out this line and we could just as easily print tapped and print a blank line so now we get a blank shell and a tapped message when i double tap the desk similar to tapping we can detect shaking as well whereas a tap is a short sharp event a shake is a more sustained event we'll copy that example code and paste that into main we have the normal setup this time we are checking if motion dot shake has crossed some threshold and if it has we print the message shaken run that script it helps to have a long picodev cable for this so you can see i can i can knock on the desk all day but we won't get a shaken event i have to actually pick the sensor up and really shake it to get that event and finally it's possible to have multiple accelerometers connected on the same pikadev bus here i have my first accelerometer with the address switch off and my second accelerometer with the address switch on to have multiple devices connected on the same bus they need to have unique address switch settings and here's what the code looks like to read from each device independently we initialize accelerometer a using the normal initialization function but this time we're passing it the asw argument and that's equal to zero because the switch is off then for accelerometer b my second accelerometer we initialize it with asw equal to one because we have set its address switch on and now we have two separate accelerometer objects in our code that we can read from independently here we're just reading the acceleration property from each accelerometer so accelerometer a dot acceleration and we return those values into x a y a and z a and similarly for accelerometer b we read the acceleration property and we apply those values to xb yb and zb and finally just so it's not too messy i'm just printing the x component acceleration for each device when we run this script we can see that they're both about the same until i start to rotate accelerometer b so its x axis is pointing upwards and for accelerometer a i will rotate that so its x-axis points downwards you can see we can read those two acceleration values independently and so we can see these are pretty versatile devices accelerometers not only can we measure linear acceleration but we can use that data to infer things like tilt angle or shaking or tapping from here you can make all sorts of useful projects like a digital spirit level that can indicate if something is sitting level or maybe even a locked box that only opens when you knock on it in the right sequence if you have some questions or just want to share something cool that you've made using the picodev 3-axis accelerometer and let us know in the comments below until next time happy making [Music]
Info
Channel: Core Electronics
Views: 2,070
Rating: undefined out of 5
Keywords: How To Use A 3-Axis Accelerometer With Microbit, How To Program A 3-Axis Accelerometer With Microbit, How To Program An Accelerometer With Microbit, How To Measure Acceleration With Microbit, How To Code An Accelerometer With Microbit, Microbit Accelerometer, Microbit 3-Axis Accelerometer, BBC Microbit, 3-Axis Accelerometer, Measure G Forces, How To, Guide, PiicoDev, Tutorial
Id: jqnTZRNiWhk
Channel Id: undefined
Length: 12min 42sec (762 seconds)
Published: Fri Aug 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.