PiicoDev 3-Axis Accelerometer LIS3DH | Guide for Raspberry Pi

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 time to build and code today i'm using a raspberry pi 4 model b and this has an sd card already set up to work like a desktop computer using raspberry pi os check the article if you need help getting started plug a picodev adapter for raspberry pi onto the gpio header on a raspberry pi 4 the ethernet arrow will point towards the ethernet connector on a pi 3b the usb and ethernet connectors are swapped connect a picodev cable to one of the ports on the pi and connect the other end to your accelerometer again make sure that the address switch asw is off and i'll connect a display connect to a network can't forget keyboard and mouse and apply power my raspberry pi is already set up to be used like a desktop computer and has the picodev drivers installed see the article if you need help with that we'll be working from the article in this guide a lot if you're not already there then find the link in the video description find the first example and copy that example code i'm going to open up the programming menu and thony ide create a new file and paste that code in and i'm going to save this to a picodev directory in my home pi directory i'll call this acceleration.py let's give the script a run we can see some data streaming up the shell here if i click the view and plotter option we can see a plot of that data and these are the three axes as being read from the accelerometer here we have the z-axis pointing straight up so that's reading about 9.8 meters per second squared as we'd expect remember an accelerometer at rest on the surface of the earth ought to read about 9.8 meters per second squared upwards if i tilt it so the y-axis points up we can watch those lines cross and now the y-axis is pointing directly up reading about 10 and i'll turn it so that the x-axis is pointing up that's pretty cool is what happens if i shake it it just goes crazy i should be able to excite just the x-axis by sliding it on the desk and the y-axis you can see a little bit of cross-coupling because no matter how hard i tried there's no way for me to perfectly excite just one of the axes and there's the z-axis for completeness let's take a look at this code we start by importing the device driver and we also import a sleep function to create a delay we initialize the accelerometer and call it motion so everywhere in this script where we see the word motion we're referring to this accelerometer then we set the range property to 2 and that's 2g this is the maximum scale that the accelerometer is capable of reading and it affects resolution you could set it to 2 4 8 and 16 g in the infinite loop we read the acceleration property that returns a tuple x y and z for acceleration in those directions and so we apply those to some x y and z variables we do a bit of rounding to round to two decimal places just to make this print look a bit nicer and then we create a string we have the x label and we concatenate that with data from the x axis and so on for the y and z axes and then we just print that constructed string moving on to the next example from the article will infer tilt angle from that acceleration data copy the next example and i will open a new script paste it in and save that as angle dot py after the same setup procedure as in the last example this time we're reading the angle property which returns three angles about the x y and z axes here we're just printing the y angle so tilt about the y axis what does that mean remembering from the right hand rule we point our right hand thumb in the direction of positive y axis and the direction our fingers curl should be the angle that we rotate for increasing angle so i should get increasing angles rotating this way and decreasing angles rotating this way let's run the script and find out where level at about 90 degrees and if i tilt this to the right that's increasing an angle and if i tilt it to the left that's decreasing in angle and if i go all the way around then it wraps at 180 degrees it wraps down to negative 180 degrees and so of course we could change this to tilt in the x-axis and now we have tilt in this axis interestingly if i tilt it this way then there's no change i only get a response if i tilt it around the x-axis now i've just changed our printed parameter to the z-angle and we've actually got some nonsense printing here think about what it means to measure tilt about the z-axis positive z is pointing straight up and so we're measuring tilt as measured in this plane and that doesn't really have meaning because gravity is going straight down so to get meaningful data about the z-axis we actually need to flip the accelerometer up onto its edge and now we can measure a meaningful angle so in a nutshell you can get data for three angles but only two of them will ever make sense for a given orientation we can detect tapping with this accelerometer find the example for detecting a tap and copy that example code into thoni into a new script i'll paste that and save it as tap dot py after the normal setup here we call the set tap method we pass it a one for the first argument for single taps and there's a threshold argument that we can tune to set how strong the tapping needs to be before a tap event is registered then in the infinite loop we query motion.tapped which will return true or false if the accelerometer detected a tap we'll print a 1 otherwise we'll print a 0. let's take it for a spin i'm printing all 0s so there's no tapping detected and if i knock the desk quite hard we get a tap and that's this threshold at play here if i change this threshold to something like 10 and re-run the script it should be a little more sensitive there we go you can see every time i tap the desk we get a waveform in that plot cool we can also detect double tapping by changing this type to a 2 to the double tap i'll increase this loop time a little bit to give us more time for that tapping now no matter how hard i knock we shouldn't get a single tap event i'll have to knock twice to get that tap event so one tap is rejected but on a double tap we get that one printed to the shell and we can see on our plotter that moment in time while a tap is a short sharp impulse a shaking is a little more sustained so we can differentiate between tapping and shaking copy that example for detecting shaking and again create a new file paste it in and i'll save this as shake dot py this time in the infinite loop we're calling the shake method and we're passing in a threshold here for how strong the shaking needs to be this time instead of printing a one or a zero we're printing a message if motion.shake returns true then we'll print shaken otherwise we'll print a blank line run the script this is where it helps to have a long picadev cable and if i shake this side to side you can see we trigger a shake event importantly though if i put it on the desk a shake is different to a tap nice now it's possible to have two accelerometers connected to the same pico dev bus here's my first accelerometer and it has the address switch in the off position when i introduce my second accelerometer it will need to have its address switch in the on position so that they can be uniquely addressed this will allow us to read data from each device independently here's the code for reading from two accelerometers we have the normal import statements and this time when we initialize accelerometer a we initialize it with the argument asw equals zero this is our first accelerometer with the address switch off or zero then when we initialize accelerometer b we initialize it with asw equals 1 because its address switch is on now we have two accelerometer instances that we can read the acceleration property of here we're reading accelerometer a dot acceleration into x a y a z a and here we're reading acceleration from accelerometer b and we're storing that result in x b y b and z b just to keep things clean we're just printing the x acceleration independently from each accelerometer so if we run that example both accelerometers are at about zero but if i tip accelerometer a to point downwards we should see one of those lines go to negative 10 and if i point accelerometer b to point upwards we should see the other accelerometer climbs to positive 10. so we can see i'm reading data in from two independent accelerometers 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] you
Info
Channel: Core Electronics
Views: 6,127
Rating: undefined out of 5
Keywords: How To Use A 3-Axis Accelerometer With Raspberry Pi, How To Program A 3-Axis Accelerometer With Raspberry Pi, How To Program An Accelerometer With Raspberry Pi, How To Measure Acceleration With Raspberry Pi, How To Code An Accelerometer With Raspberry Pi, Raspberry Pi Accelerometer, Raspberry Pi 3-Axis Accelerometer, Raspberry Pi, 3-Axis Accelerometer, Measure G Forces, How To, Guide, PiicoDev, Tutorial
Id: 1wQNX1Q9gkc
Channel Id: undefined
Length: 13min 6sec (786 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.