PiicoDev Magnetometer QMC6310 | Guide for Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the pikadev magnetometer measures the invisible magnetic fields that surround us and i'm going to show you how to get started using it with a raspberry pi we'll connect these two together and get some example code working so that we can use it just like you would a magnetic compass we'll also be able to detect whether there's a magnet nearby and at the end of the tutorial we'll do a mini project using some other picodev hardware let's get started [Music] to follow along you'll need a picadev magnetometer and adapter for raspberry pi a raspberry pi single board computer i'm using a raspberry pi 4 today and a pickative cable i'm using a longer picket of cable today so that i can keep my magnetometer farther away from my raspberry pi and if you want to do some experiments it's helpful to have a magnetic compass and maybe some magnets as well just try not to touch the magnets to the sensor we don't want to magnetize the sensor itself just play with the magnetic field around the magnets first plug your picodev adapter onto the headers of your raspberry pi and on a raspberry pi 4 the ethernet label will have an arrow that points towards the ethernet connector you can see that here connect your pcf cable to one of the picket pikadev sockets on the adapter and connect the other end to your magnetometer i've powered up my pi and connected it to the internet if you need help with that there is a raspberry pi for beginners workshop once your pi is powered on go to the start menu preferences and raspberry pi configuration under the interfaces tab make sure that we have i2c enabled open the start menu again go to the programming menu and open thony ide and we'll first make sure that picodev is installed under the tools menu select manage packages and search for picodev with two eyes here it is and just click the upgrade or install button as necessary to make sure you're working with the latest version in the article for this tutorial find the first example which is the compass and calibration example download the script with a right and save link as and i'm going to save this to a picodev directory in my home directory called compass.py back in thony open up that compass.py script that we downloaded and let's check out what's going on we start by importing the device driver for the magnetometer and we also import a sleep function we initialize the device and call that compass so anytime we refer to compass in this script we're referring to this sensor and we initialize it with a range equal to 800. that's micro tesla magnetic field strength is measured in tesla and this is the 800 micro tesla range for this device if we scroll across you can see a few other acceptable ranges next we call the calibrate function this will calibrate the sensor to make it suitable for use like a compass then in the infinite loop we call read heading and store that in a variable called heading and if we got some valid data we then print that heading to the shell let's give it a try click the green run current script button and in the shell you will see that we are now calibrating the sensor so rotate the sensor on a flat surface very slowly and we want to complete at least one full revolution there's a progress bar in the shell that's showing our progress and anytime a calibration event occurs that progress will reset it'll likely keep resetting until we finish at least one full revolution of the sensor all right i've finished the full revolution and i'll just keep going until that fills up something really cool is happening we now have in the shell a printout of the heading in degrees and we can see that in the plotter as well so if i turn the compass to the left that heading should decrease and if i turn it to the right that heading should increase let's see if we can find magnetic north okay that's about zero degrees so that means that in this studio magnetic north is that way i've taped down this paper so it can't move and i've brought out my magnetic compass so we can compare what this compass reads versus our magnetometer if i scribe a line along this compass and that means that north is in this direction or at least magnetic north i'll move that away because that needle is magnetic and it could interfere with the magnetometer we're measuring the heading that the top of the board points so if i align the side edge with the arrow we should get look at that we have zero degrees how good so that means that the magnetometer agrees with the magnetic compass that's a good start if i turn this 90 degrees to the right we're now facing 90 degrees right which is east and you can see the shell is now printing 90 degrees likewise if i rotate it now so we should be facing 180 degrees which is south and 270 degrees which is west now i've got a compass app on my phone and if i line that up with my arrow we can see that it's showing about 10 degrees which doesn't agree with either the magnetometer or the magnetic compass what's going on here a lot of applications will default to showing the true heading so magnetic north isn't actually true north depending on your location on the earth's surface there could be a difference between magnetic north and true north if i line my phone up with my magnetic north arrow and then switch the mode to magnetic north we can see that it basically agrees with the compass so magnetic north is in this direction and true north is really about this direction so here's my magnetic north line and here's my true north line as given to me by my phone and this difference in angle is called magnetic declination i'll comment out compass.calibrate because we only need to do that once and i'll uncomment this line compass.set declination so i've used an online tool to find my declination in newcastle australia as about 12 degrees depending on where you are this could be quite different but if i put that 12 degrees into compass dot set declination and rerun the script now when i bring my compass back in we ought to see if i line it up with that true north and now we're at 0 degrees and if i go back to my magnetic north line we are at about 12 or 13 degrees now you might notice that a file has appeared in your working directory called calibration.cal this is the information that we gathered during the calibration step the calibration data is saved into this file which means that we can comment out the calibrate line we don't need to call that every time we run our script whenever we initialize the sensor it checks if there is a calibration file present and if there is it just uses it you may want to periodically recalibrate your compass if you notice that the heading is no longer accurate or if you suspect that it's been magnetized by say a strong magnet speaking of strong magnets let's go to example 2 detecting a magnet find that example link right click save link as and i'll save it to the same picodev directory back in thoni i'll open up the detect magnet example and we can see that the initialization is pretty similar except this time we are initializing with a range of 3000 microtesla that's the maximum and that's just because we're going to be working with some magnets there's a threshold here set to 80 microtesla i reckon i'll change that to about 120 for now and then in the infinite loop we just call read magnitude and that will read the strength of the magnetic field we save that in a variable called strength and then we print the strength and if the strength is above some threshold we also say there's a strong magnet i've got my strong magnet here and i've run the script and we can see that we're getting about 85 microtesla and if i bring that magnet closer we should get it to trigger a strong magnet there we go great so we can detect the presence or lack of presence of a magnet i'm going to comment out this if statement and the print statement and change the sleep to 100 milliseconds the reason for that is i want to look at this graph here this plot of magnetic field strength so you can see as i bring that magnet closer and farther away that line moves an awful lot and it's quite exact you know i'm only moving this magnet a couple of centimeters back and forth and we can really see that in the plot that's really cool returning to the article download the raw data example example three so again we'll just save that link as and open it in funny this script allows you to read the raw values of each of the three axes in microtesla so we initialize very similar to every other example and we just call the read function which by default reads in microtesla if we run that script we can see some numbers printing out in the terminal but more interestingly are these plots so i have my magnet on the desk facing in this direction which means that there are lines of magnetic field flux going in this direction and so we can see x is blue and y is yellow if i turn that sensor on the table we can see both x and y reacting quite a lot but the z axis doesn't really react all that much and that's because the z axis is going through the table in this direction and there's no magnetic flux lines going in that direction yet but if i stand the magnet up on its side we can see that the z-axis shoots up that's this one here and that's because now the magnet has magnetic flux lines going through the table through that z-axis if you want to access the truly raw data off the sensor you can use the same function but with the raw equals true argument this will read the raw data from the sensor adjusted by whatever calibration offsets you have measured if there's no calibration file then this really is just the raw data straight off the magnetometer and you can see it behaves very similarly it's just on a different scale now for a mini project i've mounted a pikadev oled module onto a small platform with the magnetometer so that as i move the platform they move together back in the article find the graphical compass example and save that link return to thoni open it up and this this code is really similar to the very first compass example that we ran you might notice some similarities down the bottom where we're reading the heading for example however this code has a little bit extra it has this draw compass function which is just above the main loop this draw compass function takes in the heading in degrees and it will draw an arrow on the screen to work like a compass needle so as we move this around that needle should point north like a graphical compass and now on the display we have a compass needle with a little ball on the end to indicate the direction that it's pointing and as i rotate the whole project the angle of that needle changes to point in the same direction that our magnetic compass was pointing in the first example how good is that so that draw compass function is taking the heading in and it's basically converting that angle and some length that we describe in pixels for our compass needle it's converting that angle and length into an x y coordinate to draw the end of that compass needle the start of the compass needle is of course being drawn at the center of the screen so these are the coordinates for the end of that needle so this math is responsible for converting from polar coordinates to cartesian coordinates and then we just offset into the middle of the display width over two and height over two we draw a line from the center of the display to those calculated coordinates and then we just draw a circle at those coordinates as well so there you have it a few fun examples and a neat little project that you can do with the picadev magnetometer if you make something cool from these starter projects or if you just have some questions let us know in the comments for this article we're full-time makers and happy to help until next time thanks for watching [Music]
Info
Channel: Core Electronics
Views: 4,097
Rating: undefined out of 5
Keywords: How To Use QMC6310 Magnetometer With Raspberry Pi, How To Program QMC6310 Magnetometer With Raspberry Pi, How To Program A Magnetometer With Raspberry Pi, How To Measure Magnetic North With Raspberry Pi, How To Measure Magnetic Fields With Raspberry Pi, How To Build A Compass With Raspberry Pi, Raspberry Pi Magnetometer Measure, Raspberry Pi, QMC6310, Magnetometer, How To, Guide, PiicoDev, Compass, Magnetic Fields
Id: mfqQAHXnZpA
Channel Id: undefined
Length: 13min 6sec (786 seconds)
Published: Thu Mar 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.