How to Use a Hall Effect Sensor with Arduino (Lesson #31)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I will show you how to use hall effect sensors with an Arduino these are sensors that can measure the strength of a magnetic field unlike the read switch a type of magnetically activated switch that I cover in the previous video in our uino tutorial series Linked In the description hall effect sensors allow you to measure the strength of the magnetic field and not just whether or not a magnet is nearby in this video I'll be covering two different types of hall effect sensors analog and digital but note that they look pretty much identical and the writing on the package is very tiny and can be very difficult to read even with a magnifying glass so it is important to keep track of which type you buy and if you buy both types keep them in separate labeled containers so you don't get them mixed up I will be using analog sensor part number a1302 and digital sensor part number a3144 so if you are using different sensors you will need to double check the data sheets for your sensor for example to see if the pinouts are different but the concepts in this video should in general apply to both types of analog and digital hall effect sensors I'm going to hook up a multimeter so we can look at the difference in Behavior between the analog and digital sensors first I have the multimeter connected to the output of the digital hall effect sensor and we can see that by default when no magnet is near the sensor the output goes high or close to 5 volts you might see it fluctuate a bit as I move things around and bump wires here but when everything is that value is pretty close to 5 Vols then we will see that if I take my bar magnet and slowly start to bring it towards the sensor once I reach a certain point or a certain threshold that voltage is going to drop all the way to zero and I have my Arduino code set to turn the LED on as I back the bar magnet away once I reach a certain point the LED is going to turn off and the voltage is going to go back up to five now you might have noticed something interesting there it looks like I had to get pretty close for the LED to turn on and then the led doesn't turn off again right when I cross that threshold it actually stays on longer until I get a little farther away and we will talk about that a little more later when we look at the data sheet for this sensor but it has two different thresholds set an activation threshold for the output to switch and then a deactivation or turn off threshold where the output changes again and those two thresholds are different so I don't get the LED to go on and off when I just go back and forth past this one point it won't go off again until I get a little farther away also note that this video is not a physics lesson on magnetic fields but a quick reminder magnetic fields have a direction to them so you can imagine invisible magnetic field lines wrapping around in space from the North Pole to the South Pole of this magnet and that direction affects the sensor so we see that the digital sensor over on the left here is activated when I bring the South Pole towards the front ignore the analog sensor over on the right I just happen to be getting close enough sometimes to trigger that one too but the digital sensor is not changing the output if I bring the North Pole close to the front face of the sensor however if I switch around and go over to the back of the sensor we see the behavior is reversed the output does not toggle if I bring the South Pole near the back of the sensor but the output does change from low if I bring the North Pole towards the back of the sensor so again the polarity of your Magnet or which pole you bring towards which side of the sensor is going to determine whether or not the output changes again and for this particular sensor it works with the South Pole on the front side and the North Pole on the back side switching the multimeter over to the analog sensor we see that the behavior is a little different when there is no magnet nearby the output voltage is centered at roughly half the supply voltage so the Arduino provides about 5 volts and my output voltage is pretty close to about half that of 2.5 volts I'm getting 2.45 and then if I slowly bring the South Pole of the magnet close to the sensor we will see that voltage start to climb and if I switch it around and slowly bring the North Pole of the magnet close to the sensor we will see that voltage start to go down so here since we have an analog output so it's giving us a more continuous change instead of just going on and off this is where you can set up something where you have calibrated it to use this as a distance sensor so for a fixed magnet of certain strength you can create a calibration curve if to know how far away the magnet is based on the voltage reading you're getting or if the magnet is at a fixed distance you can use it to measure the magnetic field strength of the magnet so if you have different magnets and you want to compare their strength you can put them all at the same distance from the hall effect sensor measure the voltage and then using some information from the data sheet you can convert that to magnetic field strength we will go over that in a little bit when we look at the data sheets for these sensors so to recap we have two different types of hall effect sensors here and the underlying concept is similar but the behavior of the output is different for the digital sensor we have a Threshold at which it will be activated for a certain magnetic field strength and then a threshold at which it will turn back off and the output which I didn't have the multimeter connected to show there but remember that only ever goes between high and low and then for the analog sensor you have an output that starts out centered and goes up or down depending on the strength and direction of the magnetic field as it approaches the sensor so two different outputs that you can read with your Arduino as we'll see when we look at at the code using either the digital read or the analog read commands which one you want is going to depend on what you need to do for your project zooming in to take a look at the breadboard and wiring these and for now I have taken out the LEDs just for clarity to remove some clutter both of these sensors have three pins and looking at them from the front that is the side with the writing on it which you can't probably really see very well in this video or if you look at them from the top you see they have these tapered edges so the front side is actually a little smaller that is the front going from left to right pins 1 2 and three are power ground and Signal out so that is 5 volts from the Arduino ground and then your signal out which is going to go to one of your Arduino pins again remember that I am specifically talking about digital sensor a3144 and analog sensor a1302 in this video if you have different sensors you need to check their data sheets to get the pinouts because they might not be in the same same order as what I have here the other thing is that for the digital sensor it requires an external pull-up resistor on the output pin so I have a 10 kohm resistor going from the output pin up to 5 volts on the Arduino I then have jumper wires connected over to the Arduino for the digital sensor it is going to one of the digital pins and for the analog sensor it is going over to one of the analog input pins now that we have seen how to wire these let's switch over to the computer and take look at the data sheets and the code let's first take a look at the data sheet for the digital sensor now if you have never looked at the data sheet for an electronic part before don't worry because these can contain an overwhelming amount of information but if you are just doing an Arduino project and following an Arduino tutorial like this video you don't really need to read the entire data sheet the major thing you want to look for is the pinout diagram which should look something like this so there is a caption clarifying which side of the device you are looking at this is viewed from the Branded side or the side with the writing on it and then again from left to right we have pins 1 2 and three Supply ground and output that is specific to this part if you bought a different part number you need to find the data sheet and double check the pinout you can usually find the data sheet linked from wherever you bought the part online or if it's not there by Googling the part number in the words data sheet if you keep going in the data sheet you can find some other useful information for example this lists the supply voltage range that you can use to operate the sensor so we're using an Arduino which is 5 volts that falls well within this range but if for some reason you needed to power the sensor from a different voltage or you were doing a different non Arduino project that had a different voltage Supply this tells you the voltage range that will work with the sensor we also have the mag magnetic characteristics of the sensor in units of gaus which again I'm not really going over a magnetism tutorial in this video but I mentioned earlier that there are different thresholds so look at the little footnote here Bop or operate point is the magnetic field at which the output will turn on and then BRP is release point at which the output will turn off and there are different sensor model numbers here but for each sensor those values are different so it's not just a single threshold that toggles the output there's one threshold that turns the output on and then a different threshold that turns the output off which is why earlier in the video you saw that the LED would turn on when I brought the magnet close but then it would stay on longer as I brought the magnet farther away because of those two different thresholds now for an Arduino project where you just want to detect if a magnet is near the sensor then you probably don't care about the exact strength of the magnetic field but if for some reason you do need that information it is here in the data sheet and it would allow you to select which type of the sensor or which model number you want to buy depending on the magnetic field strength range that you need switching over to the data sheet for the analog hall effect sensor we see that this one has a lot of similar information but is arranged differently so depending on the manufacturer you might have to scroll to a different point in the data sheet to find what you are looking for in this case again the first thing we are going for is the pinout diagram make sure you look at the one for the package type that you have so we have the through hole package which has these pins that go into the breadboard that is different from the surface mount package which is designed to go flat on top of a printed circuit board so here rather than having the labels directly on the diagram we have a table where we want to make sure we're looking at the right column so it's calling the package we have here UA a and be careful because the pins are listed out of order here it goes 1 32 so pin one is the power supply so that's pin one on the left here pin two which is the third row in the table but the second pin on the part is ground and then pin three is the output so again depending on the data sheet there might be multiple packages in some table like this where you need to look up the correct Row for the pins on your package and make sure you do not get the order mixed up if you go farther down in the data sheet you will again see a ton of information which can be a little overwhelming but for most Arduino projects you do not need a lot of this one of the big ones to look for again is the supply voltage which we see for this sensor has a smaller range than the digital sensor this is only 4.5 to 6 volts but the arduino's 5 volts does fall in that range so it's fine to power one of these from an Arduino the other big one to look for for under magnetic characteristics for the analog sensor is the magnetic sensitivity this tells you how much the output voltage is going to change per unit change in the magnet magnetic field strength so we see that depending on the sensor model number that is different it is either nominally 2.5 molts per gaus or 1.3 molts per Gus that is a typical rating if you look up The Columns here that gives you a minimum typical and maximum value so for each individual sensor depending on some manufacturing variability that value can actually change and you would need to calibrate the sensor you have this Mill volts per Gauss unit again means how much does the output voltage change so remember when we had our multimeter earlier in the video the output voltage was starting at about 2.5 Vols but then it was changing as I brought the magnet closer and that amount of change depends on this value and the magnetic field strength so if the magnetic field strength changes by one Gus I would expect a change in the output voltage of 2.5 Volts for this sensor if it changes by two gal I would expect a change in the output voltage of two of sorry 5 molts for this sensor and so on if you just want to detect if a magnet is present near the sensor then you don't need this information but if you want to convert your voltage to an actual magnetic field strength in gal then that is how you do it finally let's take a look at the code which I have in a single program for both the analog and digital sensors here so going through this from top to bottom to set up the digital sensor I declare a variable for the pin I will have it connected to I also declare a variable for the pin I'll have a corresponding LED connected to and I declare a variable for the sensor reading I then do the same thing for the analog sensor but in this case the pin is one of the analog pins a zero I have an LED and I have a variable for the sensor value in my setup function I then use the pin mode command to set appropriate pins as inputs or outputs so I set the digital input pin for the digital sensor as an input I set the two LED pins as outputs and I do not need to use the pin mode command for the analog read pin so that is not there then I initialize serial communication so I can print my sensor values out to the serial monitor when I'm testing my program in my Loop function I use the digital read command to read the state of the digital sensor pin and I use the analog read command to read the analog value from the analog sensor pin I then print out the digital and analog sensor values to the serial monitor which I will demonstrate in a minute and I'm going to use those values to control the two LEDs and I do that in two different ways for the digital sensor this is very similar to what you would use if you were just using a button I use an if else statement so if the sensor output is high remember that means a magnet is not near the sensor then I'm going to turn the LED off or set it low else meaning the sensor value is low which means there is a magnet near the sensor then I'm going to set the LED High the anal anog sensor Works a little differently if you've worked with analog inputs before and if not you can check out our potentiometer tutorial back towards the beginning of our Arduino tutorial series Linked In the description of this one if you have looked at that you'll remember that the analog read command will return a value between 0 and 1,23 where 0 corresponds to Zer volts and 1023 corresponds to 5 Vols so this analog value is not just going to be high or low it's going to be some number between 0 and 1023 and remember that in my default state with no magnet nearby I was measuring about 2 and 1 12 volts or half of my Supply voltage so what I have here is an if statement set up to turn the LED on if the analog value goes outside of a certain range so if my analog value is greater than 520 or that's what this double vertical bar here means my analog value is less than 490 then I am going to turn my LED on if my analog value is between those two numbers then the LED is going to stay off so this if statement is a little more complicated than what you may be used to doing if you're just using digital inputs with high and low but again what I'm doing here is keeping the LED off as long as my analog output is centered and then I'm turning the LED on the moment it gets above or below a certain value so it's going to turn the LED on whether I bring the North Pole of a magnet close or the South Pole of a magnet close remember earlier in the video how we saw how the voltage went up or down depending on which pole of the magnet we brought close to the sensor this is where the serial monitor is useful for calibrating your sensor and setting this range so I have opened up my serial monitor here and it is printing out both the digital and analog values so right now I have no magnets near either one of the sensors and you see the digital value is one or high and the analog value has a little bit of noise but it's bouncing around a value of about 509 or 510 if I take the South Pole of a magnet and bring it near the digital sensor we will see that switches low or to zero and we'll see that if I move it over a little closer to the analog sensor the analog value has gone up to about 5 2530 I'm going to take that away again flip it around and do the North Pole and remember that the North Pole was not having an effect when I bring it towards the front of the digital sensor which is what I'm doing now that's staying high but we can see when the North Pole gets near the front of the analog sensor that this reading is going down to around 490 so this is going to vary depending on the strength of your magnet and how close you bring it to the sensor but you can use that calibration procedure to decide what variables or what values you want for your ranges here to cause your analog sensor to do something of course controlling LEDs is just one example of what you can do with these sensors once you know how to set these sensors up and take readings from them you could use them to control other outputs like buzzers or Motors you can check out tutorials for different output options in our Arduino playlist Linked In the description of this video finally I have been demonstrating with a bar magnet just for clarity in this video but you should be able to use many other types of magnets with these sensors for example here I have a magnetic pet collar tag where the polarity is not really obvious it's not marked anywhere on the tag but you can see that it will still activate the sensors if I bring it close enough so if you wanted to build something like an automatic pet door or tracker to detect when your pet gets close to something or goes in and out of a certain opening you could use this type of sensor to detect the presence of one of these magnetic tags for more Arduino tutorials science projects you can do with an Arduino and over a thousand other projects in all areas of Science and Engineering check out the links in the description and visit our website www.sciencebuddies.org
Info
Channel: Science Buddies
Views: 13,733
Rating: undefined out of 5
Keywords: arduino, reed switch
Id: q4BkhwoGzbM
Channel Id: undefined
Length: 19min 47sec (1187 seconds)
Published: Fri Jan 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.