Sensing Orientation With The ADXL335 + Arduino

Friday, April 22 nd , 2011

I know, I know, this one has such a simple name. Where’s the pun? Honestly, the description was just to long to include one. Maybe it could have been “What’s Up? Sensing Orientation With The ADXL335 + Arduino” – Ehhh… Probably not.

A few weeks ago we wrote a tutorial about using the ADXL345 to sense taps, and drops, but this week we are going to wind it back, go back to basics, and show you how to take an analog 3-axis accelerometer and use it to sense simple orientation with it, specifically the ADXL335, but this can be applied to any analog 3-axis accelerometer.

Before we really dive into it, we need a general understanding of how accelerometers work, so if you already know why each axis can only differentiate 180deg of movement, you can skip this. Wraning: Over-simplification ahead. In the last article I described an accelerometer as a device that sensed movement. Today, think of them as devices that sense gravity.

Basic Understanding Of Accelerometers

Accelerometers measure acceleration, often caused by motion. But when they are standing still, the only acceleration the accelerometer senses is due to gravity pulling down on it.

Imagine a box that has little springs sticking straight out from the sides of the box, and that the accelerometer measures how hard gravity is stretching out those springs. The springs on the side are all bending the same amount, the spring on the bottom is all stretched out, and the one at the top is not stretched at all (because the spring is pull back into itself), so the accelerometer sees it as feeling no gravity, or 0g (gravity). If you rotate the box 90º and follow the spring on the top. It is now on the side and is hanging down some and the sensor sees it now feels .5g. Rotate 90º again, and it is at the bottom, stretched out, and it feels 1g. Rotate again 90º and we are at the side again, with it feeling .5g, and 90º rotation again, we are back at the top and it feels 0g. So we made a full rotation and the accelerometer saw this: 0g-> .5g -> 1g -> .5g -> 0g.

If you look at this, it means that the accelerometer can really only sense differences in 180º of movement as the other 180deg is just a mirror image. So how can we sense 360 deg of movement?

The trick to this is that while one axis can only sense a 180deg difference, so can the another axis, but they sense it differently. If you look at the chart to the right, you can see the sensed values while rotating round the X Axis. (The x never changes because it is always facing the same direction) – So we can combine the y, and z values to find x using a trigonometry function called Atan2 who then gives us values back as -180º to 180º (but in radians, so it is -π to π and well have to convent it).

No Love For Yaw

Yaw is name for rotation around an axis that is similar to spinning a top, or shaking your head “no.” Accelerometers can’t measure this type of motion. Why? Well think about the box. If you turn the box in this manner, non of the sides change their orientation to the ground. If you need to measure yaw, you will need to incorporate a gyro or digital compass into your project, but it gets tricky, and is beyond the scope of this article.

Why So Shaky?

The accelerometer is really going to report a proper measurement when it is standing still. if it is shaken, moved, bumped, or in free fall, the acceleration the accelerometer measures is no longer purely gravity based, and you are going to see that in your readings.

But I Need A Cleaner Reading

If you need a clean reading during movement, you need something this article won’t provide, and that is a gyro and an accelerometer working in combination. Together they form something called an IMU – Inertial measurement unit. Correctly setup, and gyro is able to kick in where an accelerometer leave off, and vise versa. A gyro is great at measuring rotation, but has no understanding of orientation. And an accelerometer is good at determining orientation, but has no ability to keep track during rotation.

Hooking It Up

Connecting the ADXL335 to your Arduino is a snap. It powers off of the 3.3v, and the x,y,z connectors just connect to the 0,1,2 analog input pins. This accelerometer is a very simple and just outputs an analog voltage depending on the sensed value.

Code For The ADXL335 Or Other Analog Accelerometer

So here is some code: This code simply calculates 0-360º values for X,Y & Z and prints it to the serial terminal. This code is not specific to the ADXL335, and with almost no tweaking you can make this work with any analog 3-axis accelerometer.

Our Arduino’s analog input pins are comparing the analog voltage from the accelerometer’s analog outputs to 5v, and assigning number to that. If the Arduino senses 0V it reports 0, and 5V it reports 1023 ( you read more in the ADC Article in our wiki). The highest value I read from the sensor when it was standing still was 402, so that is 1g, and the lowest was 265, so that is 0g. If you get funny output from the code, you will need to change this depending on the highest and lowest values you get from your accelerometer. (Ask us the discussion forum for help if you need it.) Just changing these values will make the code work for any other analog 3 axis accelerometer, not just the ADXL335.

Because 265 (0g) was sensed at 0º, and 402 (1g) was sensed at 180º we are going to convert those values accordingly. But atan2, likes using -90 to 90 better, so we will do that instead. Lastly we convert the atan2 radian output to degrees, and voila, we have 360º measurements!

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the ADXL335, prints calculated orientation via serial
//////////////////////////////////////////////////////////////////


//Analog read pins
const int xPin = 0;
const int yPin = 1;
const int zPin = 2;

//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 265;
int maxVal = 402;


//to hold the caculated values
double x;
double y;
double z;


void setup(){
  Serial.begin(9600); 
}


void loop(){

  //read the analog values from the accelerometer
  int xRead = analogRead(xPin);
  int yRead = analogRead(yPin);
  int zRead = analogRead(zPin);

  //convert read values to degrees -90 to 90 - Needed for atan2
  int xAng = map(xRead, minVal, maxVal, -90, 90);
  int yAng = map(yRead, minVal, maxVal, -90, 90);
  int zAng = map(zRead, minVal, maxVal, -90, 90);

  //Caculate 360deg values like so: atan2(-yAng, -zAng)
  //atan2 outputs the value of -π to π (radians)
  //We are then converting the radians to degrees
  x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
  y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);

  //Output the caculations
  Serial.print("x: ");
  Serial.print(x);
  Serial.print(" | y: ");
  Serial.print(y);
  Serial.print(" | z: ");
  Serial.println(z);

  delay(100);//just here to slow down the serial output - Easier to read
}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.

Extending This

Even with the limitations of the ADXL335, you can do some great things. This is one time that I think just hooking a RGB LED to your Arduino with this makes a great device. Just kinda fun to play with, mixing colors as you turn it, so if you must use LEDs with this, I’m ok with that.

This can really come in handy when you want to control anything based on orientation. So, get your tools ready, and build a reclining chair that knows when you reclined, and turns on the TV for you, OR… use it to master your posture as you balance a book on your head while recording the values.

Just a note: I had issues with connecting this with a servo. The servo was taking too much power from the Arduino, and the it was really throwing off the analog readings. So just note that. I did read that the servo needed a decoupling capacitor to work well, but I have not tried it.

We need your help

bildr is in need of people interested in helping write any sort of blog post for bildr, not just tutorials. If you think you would like to help bildr by writing something, supplying code or schematic, or just have an idea for an article or other post you think should be written, please contact us at blog@bildr.org or let us know in the forum.

A Swarm of Xbees! Arduino Xbee Wireless & More

Friday, April 15 th , 2011

In the past we have covered a few things that interact through serial, from RFID readers to controlling an Arduino’s pins using the serial terminal. Serial as we have talked about it is actually know as UART, and operates over 2 pins RX and TX (receive and transmit). These connections have previously been limited by their required wires. So… what if you could drop the wire connection between the arduino and the RFID reader, or your computer and the arduino without adding much complexity? Well that is what Gerardo proposed with this article he wrote for bildr.

Everyone hates long wires running from one end of the room to the other, and let’s face it, it’s aesthetically unpleasant. So, why not make your next project a little bit more interesting and easy to use by adding wireless communication capabilities? In this post we will be using the the series 1 XBee modules to make this happen. This particular version of the XBee is perfect for applications where you need low-power consumption, easy integration, and great distance.

Series 1 vs Series 2/.25

A quick note here: This article does not cover the series 2 or series 2.5 XBee modules, and the series 1 and 2/2.5 are not compatible with each other. If you are looking to create mesh-networks, or multi-point to single-point networks, this is not for you, and you want the series 2/2.5. If you want ease-of-use, have low security requirements, and are basically looking to replace your wired serial connection with a wireless solution, the series 1 XBee is perfect.

Furthermore, the series 2/2.5 is not meant to replace the series 1… They are just different and live side by side, so don’t feel like you are buying obsolete technology with the series 1.

As I alluded to above, these modules are basically a wireless UART serial connection, so you can communicate with anything that uses UART (has the RX/TX pins). And when series 1 XBees are properly configured, sending data through the RX pin on modules will cause that exact same data to just come pouring out the TX pin on all other XBees in range as if they were wired together. Simple as that.

Choosing the right one for you

XBees come with different antennas, and power ratings, so depending on what your requirements are, the modules will cost you anywhere from $20 to $70 and are available from your favorite vendor like Sparkfun. If you are not sure what you need, go with the 1mW chip antenna version, and you usually can’t go wrong.

Each module has its own advantages and disadvantages, and you should choose them considering your individual requirements.

  • Wire antennas offer omnidirectional (When the antenna is straight, the maximum transmission distance is equal in all directions.)
  • The chip antenna is a flat ceramic chip. It takes up less space than the wire antenna, but the signal is not as omnidirectional as the wire antenna.
  • The U.FL connector is the smallest of the external antenna connectors. You can use the XBee without attaching an antenna to this connector, but if your project is inside a metal case, you will need to attach an antenna and make sure it pops out. If you don’t, the signal will be attenuated by the metallic case.

getting started

At the very minimum, you are going to need at least 2 XBee modules… But we also reccomend using either an XBee shield, or XBee ready Arduino, as well as an XBee explorer if you want to change the XBee’s default configuration. So, yes, it can get expensive, but you are opening a lot of new possibilities for your projects.

Hooking one up

The supply voltage for the XBees is 3.3 volts, and it is a strict 3.3v. Sending a 5v serial commend to them from your Arduino can flat out kill them, so pay close attention when connecting so you don’t fry your circuits.

Arduino in its many forms has made this easy for you, you can purchase an XBee shield that will take care of all of this, or they even sell XBee ready Arduinos like the Arduino Fio (requires an external usb programmer). These guys are going to do everything for you as far as getting the XBee connected to your arduino goes.

If you are using an Arduino and have no XBee shield, or you want to go completely Arduino-less, you still have options. If your arduino is a lillypad or other 3.3v arduino this will be easier, and nothing else is really needed. If you are like 95% of us and all your arduino’s are 5v arduinos, this is only slightly harder. The 5v arduinos like the duemilanove and the uno all communicate at 5v. That means that even if you power something up using the 3.3v pin, the serial, digital pins, everything, are all still operating at 5v. Sometimes this is not an issue, but with the XBee it is. But there is a way to convert the 5v to 3.3v using something called a logic level converter. The logic level converter steps the 5v down to 3.3v for safe communication. It also works the other way around, the 3.3v serial from the xBee will be converted to 5v serial for the Arduino.

If you decide to go this way, note, the XBee is not breadboard friendly, so you may want a breakout board unless you want to solder directly to the pins. Sparkfun does offer a board that acts as a brekout board and logic level converter in one, but, based on the comments, it sounds like a lot of people are having issues with it, so be forewarned.

The logic level converter needs to be powered by both 5v and 3.3v, and has several ins/outs. So look to the schematic on the right to get this all figured out.

The XBee itself really only needs 4 connections to operate. Power, ground, and RX/TX. If you have this hooked up to a sensor that is only transmitting data (Like the RFID reader), you could even get away with 3 connections. The XBee has many other pins on it like read/write LED pins so you can see when it is communicating, but these are’t required.

Configuring the XBees

XBees are actually ready to use out of the box. They all come configured on channel 0X0C (12), operating on network 3332, and use a default baud rate of 9600bps. What this means is that without changing these settings, they will just talk to each other. But it also means that anyone else (in range) who also hasn’t changed these setting can communicate with them as well. If this is not an issue for you, you can actually skip the rest, you are done. Just remember that someone’s command to read a value, could be your command to self-destruct, so if you wonder why your robot just jumped off a cliff, it could be because someone with the same settings just came into range. So… if you would like to change these settings and make your wireless a bit more secure, keep reading.

To reconfigure your XBee, you are going to need a way to connect it with your computer. For this we use the XBee Explorer USB. This requires the FTDI drivers to work, but of your computer is already setup to wotk with a pre-UNO arduino, you actually already have this installed. To talk to the XBee you will need the XCTU software (windows), or a serial terminal like ZTERM. The arduino software does have a serial terminal, but it wont work for this. (it needs to be able to transmit without line end characters)

To reconfigure the XBee, first plug the XBee onto the explorer making sure it is facing the right direction (use the white outline as a reference) then plugin the explorer to your computer.

XBee shield on top of Arduino with LED and potentiometer for code example 2

3.3v Arduino Pro connected directly to the XBee Module

5V Arduino connected to XBee using a Logic Level Converter

ID-20 RFID reader, sending data through XBee without a microcontroller

Windows Computer

First, download and open the XCTU application. Check the different tabs at the top. From PC Settings, you will have to choose the correct COM port. After selecting it, you should see basic information on your current configuration (baud rate, flow control, data bits, parity and stop bits). The Range Test tab allows you to check the communication range. This range depends on the materials interfering with the line of sight (walls, furniture, etc.) and other signals in the environment, portable phones, microwaves, other XBee networks… From the Terminal tab you will be able to send data and see any received information. The Modem Configuration tab will allow you to make changes to the current configuration.

For this tutorial we will be focusing on the PC Settings, Terminal, and Modem Configuration tabs, but feel free to explore the Range Test tab. After selecting the correct COM port in PC Settings and checking out the basic information, click the Test/Query button. A window should (see picture blow) pop up indicating some information about your XBee. If nothing pops up, try again, and make sure the xbee and explorer are connected correctly. If it still does not work, your XBee could be malfunctioning and may need to contact the manufacturer for support.

Now open the Modem Configuration tab, and click on the Read button to check the current configuration.

At this point you may be required to download some updates. If this is the case, click on the “Download new versions” button. Use the web as the source, and wait for the download. After the updates are installed, press the Read button.

The white box should start populating with the actual configurations. In order to change them, double click on the value and entered in the desired value. If you want, you can up the baud rate in order to speed up transmission.

Make sure to note the channel your XBee is working on. Once finished with the first xbee, do the same to the second making sure the channel, network id, and baud rate are the same. All of your XBees will need to be have the same channel and the same settings under “Serial Interfacing” to be able to communicate with each other. Different channels are available in case there are other XBees projects nearby as even operating on different network IDs, nearby XBees on the same channel can cause interference and reduce the achievable distance.

You should now have both XBees with the same configuration. Leave one xbee on the explorer and mount the shield on top of the Arduino, but do not place the xbee on the shield (or connect it to your arduino) just yet.

Non-Windows computer

Unfortunately the XCTU application is windows only, but if you are on a mac, or linux, you are not at a complete loss. Using your favorite terminal application you can do most of the configuration via the command line. For this demo, I we will be using ZTerm for Mac OSX.

After you have the XBee connected to the explorer, and the explorer connected to to your computer, go ahead and open ZTerm. A window may pop up asking what you what serial port you would like to connect to, select the one with the funny name, it should start with “usbserial” – Mine was usbserial-A8007ZVm. If it does not come up, after zterm has finished launching, go to the settings menu and select “Modem Preferences”. Under “Serial Port” select the one as described above (image below). Now go back to the settings menu and select “Connection…” Here we need to change a few things. Make sure to configure this just as I have mine in the image below, especially making sure that the baud rate is set 9600. The terminal should be all ready to communicate with the XBee.

Now that that is all done, we are going to send commands to the XBee to set configurations. This will feel a little weird at first, but you will be ok. When you type, you will not see it in the terminal window. This is normal! Some of the commands you do not press enter after, so remember that! Also, if you dont enter commands after a few seconds, you have to nudge it again by entering the attention command again.

You can change a lot of the setting from here, but im only going to cover changing the network ID. For more information on changing other settings through the terminal check out this page.

In the terminal window we are going to call the attention of the XBee by typing +++ DO NOT PRESS ENTER – but you should see “OK” appear once you do it. If you wait too long before you type another command, the XBee will stop responding to your requests. If this happens, just type +++ again.

First, just to make sure it is all working, type ATID and hit enter (Henceforth noted as <enter>). It should read back 3332, this is your network ID – 3332 is that default we talked about. To change this, type ATID and the 4 digit ID you would like to use and hit enter. Like ATID1234<enter> or ATID2600<enter>, and it should read back OK. If it looks like it says OK32, it is because zterm does not clear the line, so the 32 is left over. Type ATID<enter> again and it should read back the network ID you set using ATID. Now that we set the Network ID, we need to save it. To save the new settings type ATWR<enter>.

Now that the settings are saved, you need to apply these same settings all XBee modules you want on the same network, or they wont be able to communicate with each other.

Post Configuration

Now that all of your XBee modules are all set up and on the same network the way this will work is that any incoming data on that RX pin on any XBees on the network will cause that same data to come out of the TX pin of all the other modules on the network and vice versa. So… If you have 10 XBees on that network, they will all receive any data that any of them transmit. This is pretty cool I know! Just note that the serial buffer can get filled pretty quickly so if you are transmitting too much data constantly, you can get lost packets and some of the data may not transmit properly.

Also, if you have an XBee on an xbee explorer connected to your computer, and you have your arduino serial monitor open (you will need to select the explorer just as you would an arduino to be able to read from it), you can send and receive commands to the xbees on that network using this. This also means that the wireless RFID diagram above could send that RFID tag to an XBee connected to an Arduino, or a computer.

So we have a few code examples that show both of these happening.

Code

Because the Arduino is actually programmed through the same RX/TX pins the xbee will be using, it is not possible to program your Arduino while the XBee is connected to it. So you need to make sure to remove the XBee from the Arduino when you go to upload a new sketch.

For our first code example, we have put together an extremely simple computer to Arduino sketch where we are going to send data from the computer to the Arduino and vice versa. Sending “a” from the computer terminal window will turn on the LED on pin 13 of the Arduino, and sending “b” will turn it off.

You can also send this data from XCTU as well in the Terminal tab. Type an ‘a’. Check your Arduino’s LED under the shield. It should be on. Type ‘b’ to turn it off. You can also send data from the Arduino to the computer just as you normally would using Serial.print(). This can be really helpful in getting a response back after you send a command.

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Monitors the serial port to see if there is data available.
//'a' turns on the LED on pin 13. 'b' turns it off.
//////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);    	//initialize serial
  pinMode(13, OUTPUT);   	//set pin 13 as output
}

void loop() {
  while(Serial.available()){  //is there anything to read?
	char getData = Serial.read();  //if yes, read it

	if(getData == 'a'){  	 
  	  digitalWrite(13, HIGH);
	}else if(getData == 'b'){
  	  digitalWrite(13, LOW);
	}
  }
}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.


The second example requires 2 or more Arduino/XBee combos that are exactly the same. See the diagram above with the potentiometer and LED connected to it.

What this does: Turning the potentiometer on one Arduino, will change the brightness of all the others on the network.

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Turning the pot on one Arduino, dims the LED on all the others on the network
//////////////////////////////////////////////////////////////////

#define potPin 3
#define ledPin 11


int inByte = -1;
char inString[6];
int stringPos = 0; 
int lastValue = 0;


void setup()  { 
  pinMode(ledPin, OUTPUT); 
  Serial.begin(9600); 
} 

void loop() { 
   inByte = Serial.read();

   //only send new value if it is different than last sent value.
   //Min value change of 5 neded. Just so we dont flood the network
   int potVal = analogRead(potPin);
   if( abs(potVal - lastValue) > 5){
     Serial.println(potVal);  
     lastValue = potVal;
   }

  //if there is any numerical serial available, store that
  if((inByte >= '0') && (inByte <= '9')){
    inString[stringPos] = inByte;
    stringPos ++;
  }

  //if there is a line end character, this string is done, take value and write to LED pin
  //clear the string when done
  if(inByte == '\r'){
    int brightness = atoi(inString); //convert string to int

    //incoming will be a range of 0-1023, we need 0-255
    brightness = map(brightness, 0, 1023, 0, 255);
    analogWrite(ledPin, brightness);


    //clear the values from inString
    for (int c = 0; c < stringPos; c++){
      inString[c] = 0;
    }
    stringPos = 0;
  }

}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.

Extending This

Now that you have the basis on XBees, try working with more than two XBees. Create different network topologies and try different configurations. Explore how walls and other objects affect the range of the signal transmission. If you really cant think of something right away, use two arduinos to make a wireless RFID door lock converting this tutorial. Or take control of your Arduino’s pins wirelessly converting this tutorial. But really, who cant think of something that could be made cooler using wireless?

About The Author


Gerardo J. Ochoa
I am a computer engineering student working part-time as an embedded software developer. My technical interests include HCI, embedded systems, and basically anything with LEDs

We need your help

bildr is in need of people interested in helping write any sort of blog post for bildr just as Gerardo did here. If you think you would like to help bildr by writing something, supplying code or schematic, or just have an idea for an article you think should be written, please contact us at blog@bildr.org or let us know in the forum.

A Strange Attraction. Various Hall Effect Sensors

Tuesday, April 5 th , 2011

I came pretty close to calling this one “Magnets, How do they work?” But I thought that might confuse more than it would amuse. All jokes aside, hall effect sensors are pretty cool, and also pretty simple to use, but there are a few types you may not know about and I figured it would be a good time to introduce them incase your next project was In need of sensing some magnetic forces.

If you don’t know, a hall-effect sensor is a device that reacts to a magnetic field. The most common reaction being the sensor acting as a digital switch “turning on” in the presence of a strong enough magnetic field. And actually most people when first looking into them only know of these. But the reality is that there any many different types, from ones that output an analog signal depending on the strength and polarity of a magnet, to ones that latch on when a strong enough field is sensed, but won’t unlatch until an equally strong, opposing field is sensed.

Hall effect sensors are all rated to work with a specific strength of magnet field (measured in Gauss) which dissipates with distance from the magnet. Just like sound, it is softer when you are further away from the source. The only reason this is important, is because the distance a magnet needs to get to one of these sensors to trigger it is totally dependent of the magnets strength. So a really powerful magnet will trigger the sensor from a greater distance than a weak magnet. Sounds obvious I know, but these sensors also have a max Gauss rating as well. So, too powerful of a magnet, getting too close, can mess them up and eventually break them. Most sensors are sold in a wide range of sensitivity. The 3 in this article work well with household magnets, but if you need ones to work with very strong magnets, they are available.

The 3 sensors we will be going over are:
OPTEK Technology OH090U – Simple Hall Effect Sensor
Melexis US1881 – Latching Hall Effect Sensor
Allegro A1321 – Ratiometric Linear Hall Effect Sensor

OPTEK Technology OH090U

The OH090U (available from digikey at $1.75) is probably what you think of when you think of a hall effect sensor, this guy acts just like a digital reed switch, when a magnetic field gets close enough, it switches. In the case of the OH090U, it switches LOW. So when no magnetic field of the minimum trigger value is sensed, the OH090U output pin will be equal to the source voltage (4.5V to 24V), and when a field of the minimum trigger strength is within range, the output pin will be ground, or 0v.

One nice thing about the OH090U is that it schmitt triggers. Meaning there is a gap between when it triggers on, and triggers off. So if the sensor triggers on at 90 gauss, it wont release until it drops below 65 gauss. This keeps it from triggering on/off/on/off if a magnet is right on the cusp of that minimum trigger spot.

Hooking It Up

The doc sheet for the OH090U says you need a capacitor between ground and the out pin, however, in my testing, it worked perfectly without it. This is probably to smooth out the signal, and 95% of the time, this is going to be ok with out the capacitor, but if you plan on using this for automative purposes or other situations that could put people in harm’s way if it were to misfire, use the capacitor, seriously. The sensor does need a 500-860Ω resistor between VCC (V+) and the output pin for it to work.

Melexis US1881

The Melexis US1881 (available from sparkfun at $0.95) is a Latching Hall Effect Sensor meaning that once it is triggered it latches and will not unlatch until a magnetic force of reverse polarity and strength is sensed. So If the north pole of a magnet turned it on, the south pole of a magnet is then needed to turn it off. When the US1881 is triggered the output will be equal to the source voltage (3.5 to 24V) and unlatched will output 0v/ground.

I did find that using an overly strong magnet with this sensor would sometimes not latch it or would latch it to the point that it was hard to unlatch. So keep your 1in rare earth magnets away from this one.

Hooking It Up

Just like the OH090U above, the US1881′s documentation recommends some capacitors from ground to output and VCC. But again, I tested it without them, and as long as you have a 10K resistor between VCC and the output, it will work for 95% of what you need to do. From what I could tell, the capacitors are just there to smooth the output and stop the signal from bouncing, but most microcontrollers like the Arduino will not have any issue with this. If you find that the sensor seems to be triggering more than once each time it should, try putting the capacitors in, it could be your problem.

Allegro A1321

The Allegro A1321 (available from modern device at $2.60) is a linear hall effect sensor. Unlike the other two, this sensor does not have a wide operating voltage range, and is limited to 4.5 to 5.5v (Assuming 5v for article). When no minimum magnetic feild is sensed, this sensor will output half of the source voltage, (2.5v). When the north side of a magnet approaches the face of the sensor, the output voltage will linearly ramp up to the source (5v) and when the south pole of a magnet approaches the face of the A1321, the output will linearly drop to ground (0V).

Hooking It Up

Unlike the other 2 sensors, no external parts are needed to use the A1321, just connect 5v to pin 1, ground to pin 2, and pin 3 will output a linear voltage as described above.

Video

The behaviors of all of the above sensors are shown in this video.

Extending this

Magnets are really cool, on their own, but mixed with hall effect sensors they can do some pretty cool things, like levitation! You can use them to actuality count when a wheel or gear rotates by placing a small magnet on the end of the wheel or gear. The A1321 can be used to sense when a lamp is turned on by placing it over the cord. When the lamp turns on a small magnetic field is produced, enough to be sensed. In my research it seems companies are really trying to push their use in steering assemblies for cars, acting as high precision rotary encoders. But don’t do something so boring like that…. create secret locks that require you to place a magnet on a certain spot to open, fill a floor with hundreds of them and place magnets on your shoes and watch the floor light up where ever you walk. WAIT…

Use the latching ones, get like hundreds of them, and place them with LEDs under a resin floor. Put north facing magnets on your shoes, and south on other people’s shoes and see when you have walked, but others have not! Yeah, it’s late.

We need your help

bildr is in need of people interested in helping write any sort of blog post for bildr. If you think you would like to help bildr by writing something, supplying code or schematic, or just have an idea for an article you think should be written, please contact us at blog@bildr.org or let us know in the forum.