Gas station without pumps

2012 November 20

Physics lab with magnetometer

Filed under: home school,magnetometer — gasstationwithoutpumps @ 16:10
Tags: , , ,

Today we did not go over homework problems in our weekly physics time, but did a lab instead.  I rewrote the code I had for using the Freescale MAG 3110 magnetometer chip on the Sparkfun breakout board, so that I could zero-out the measurement for easier differential measurement.  What we did then was to make a big loop of wire (26 gauge magnet wire—the same wire we used for our Young’s modulus experiment) from the floor to the ceiling beams, holding it to the floor with a couple of books, so that we had a straight vertical wire in the middle of the room, several feet from other wires.  We put the magnetometer near the wire, recording the distance and the orientation of the sensor, then zeroed the reading and measured the magnetic field 3 times. We then put a current through the wire (about 200 mA), measuring the current and the magnetometer reading (again, 3 sets of x,y,z values).  We did this for several distances and magnetometer orientations.

We also did one pair of measurements without the wire, zeroing the sensor in one orientation, then turning it around 180° and making another measurement.  This should give us an independent check of the units that the sensor readings are in, since we expect this measurement to be twice the strength of the Earth’s magnetic field (according to the World Magnetic model, as displayed in Wikipedia, we should have about 49µT at a 60° inclination, so the horizontal component should be about 24.5µT, and our measurements should show about a 49µT difference).  Of course, the rotation may not have been an exact 180° horizontal rotation, so we can’t really use this measurement to calibrate the sensor.

The manufacturer’s data sheet claims that the resolution is 0.1µT, but we recorded the sum of 10 successive readings, so our units are 0.01µT.  The repeatability of the measurements was not too bad—probably around ±0.5µT (I’ll have my son compute standard deviations for each set of 3 readings).  We only measured out to 5cm from the wire, since at that distance the field we were measuring seemed to be buried in noise.

My son’s task is to take the recorded field measurements and plot field strength as a function of distance for the measurements we made (probably correcting for differences in current, if those are large enough to matter).  He should also compute the expected magnetic field around a long wire for that field. There are several measurements at one distance, as we tried to verify that we were reading the orientation of the chip correctly—that distance might be a particularly good one for comparing the measured and computed field strengths.

Next week, we’ll try to do a little homework comparison, but we’ll also do a lab winding a helix of wire and measuring the field around it.  We’ll use the computational problem (18P79) to compute the expected field in different places, and try measuring the wound solenoid in corresponding locations.  This means that in setting up the program we’ll have to make the number of turns, the radius of the solenoid, its length, and the current through the solenoid all easily changed, to match the simulation to the coil that we wind.



2012 November 14

Chapter 17 done, on to magnetic fields

Filed under: home school,magnetometer — gasstationwithoutpumps @ 20:53
Tags: , , , ,

Chapter 17 of Matter and Interactions, “Electric Potential”, went very quickly.  I had allocated 3 weeks to it back when I was making up a schedule, but we polished it off in a week.  When we went over the problems yesterday, there were a few discrepancies between the results my son got and the results I got.

On 17p45 and 17p47, which involved electron guns with high voltage, he had computed the electron velocity without a relativistic correction, while I had done the calculation with the correction.  Since speeds were around a quarter the speed of light, the relativistic correction makes a difference, but not a huge one.

On three other problems, the discrepancies were all errors on my part (mainly sign of electric field), though in one case (17p80) it took us a while to find where I had goofed, though it was immediately obvious that his sign was right and mine wrong, just by looking at the extreme case, where the potential was being measured right at the charge whose sign was reversed.  I had forgotten that the field pointed down in the region of interest, and so left out one negative sign.

Chapter 17 went quickly in part because there were no experiments I could think of to do.  About the only relevant experiment I can think of would be to build a Van de Graaff generator (or other electrostatic generator).  That might be fun to do,  but would take more time than I want for one experiment. So we’re closing Chapter 17 after only one week, and racing on to Chapter 18, on magnetic fields.

I’ve not finished reading Chapter 18 yet, but I can see several experiments we can do.  We can do the 5th-grade science experiments that the book suggests—I even have some of the wires and compasses salvaged from the 5th grade classroom when my wife’s school did a massive cleanup this summer (half the building had to be vacated for construction after the July 4 fire). But there is no reason to stop there.  I have multimeters and a MAG3110 magnetometer, and we’ve previously written code to re-center the magnetometer readings, so we can actually measure currents and magnetic fields.   We might not even need to do the centering of the magnetometer, since we can do measurement of the magnetic field at a location as a series of different measurements with different currents, and look just at the differences in the readings.  The full-scale range of the magnetometer is supposed to be ±1000 µTesla (the Earth’s magnetic field is about 25 to 65 µTesla at the surface) with sensitivity of about 0.1µTesla, so the magnetometer should be far more sensitive than using little compasses.  It also measures the field along 3 axes, so we can look at the vector for the field, not just measure in one plane.

I’ve not finished reading Chapter 18 yet, so I’m not quite ready to assign problems, but I think that the computational problem for 18P79 (simulating the magnetic field of a solenoid) is worth doing, particularly if we compare the results from using a number of parallel rings to the results from using a helix, though I’d be satisfied with just the helical simulation.  We don’t get to inductance until Chapter 23, but we may want to wind a coil that matches our simulation and measure the magnetic field from it.  We’ll have to add some series resistance to make sure that we don’t fry our wall-wart power supplies (nor turn our solenoid into a fuse), but we should be able to wind a coil on a cardboard tube and measure the field in various locations with the magnetometer.  We should probably start with measuring the field around a straight wire first, though.

If we use a long piece of wire (say L=1m) and measure close to it, (say r=1cm), we should see a field of  about \frac{\mu_0}{4 \pi}\frac{2 I}{r}, so to see 2 µT, we’d need I = 0.5 * 2E-6 T * 0.01 m / (1e-7 T m/A) = 0.1 A, which is quite a reasonable value to produce from a battery or wall wart. With a 12v supply and a 22Ω resistor, I could provide 0.54A, except that the 22Ω resistor I have is only a 2w resistor and would get too hot trying to dissipate 6.5w.  Actually, I’m not really sure what the rating of the resistor is—it is 18mm long and 8mm in diameter, most likely a carbon resistor, and the only resistors that size I found online were 2W resistors. With a 5V supply, we’d get about 0.23A or 1.14w, and stay well below the 2w limit, while still getting a magnetic field that we could measure 1cm away (about 45 µT).  By using the dimensions of the MAG 3110 breakout board to set distances, we could measure fairly reliably at distances of 0.5 mm, 2mm, 6mm, and 8mm from the wire. (I’m not quite sure about the 0.5mm and 2mm—the breakout board+chip is 2.5mm, and the board alone is 1.5mm, but I don’t know where in the 1mm thick package the magnetometer sensor really is—we could use the 1/r dependence of the magnetic field strength to try to figure that out.)

So next week: string up a longish wire, add a 22Ω series resistor, a 5V power supply, an ammeter, and a switch, then measure the current and the magnetic field at various distances.  If we get that working, wind a solenoid, measure the field around it, and compare the measured field to a simulation.

2012 May 22

Links for Global Physics Department

Filed under: — gasstationwithoutpumps @ 11:55

I’m chatting with the Global Physics Department on Wed 23 May 2012.  This page is a collection of links for them. I have a lot more posts that I’ve not included here (see posts tagged with “physics” and posts tagged with Arduino, for example).

Choosing to Homeschool

Posts from my blog as we just started up home schooling:

Physics books

Blog posts about physics books (not in chronological order)

Physics labs

Tracker video analysis

The students did not end up doing any video analysis on their own, but I put some time into trying to use Tracker and into improving the software.  I can see how video analysis could be a good tool in a physics class, but the process was too slow for our very limited class time, and the students never got excited enough to do it on their own time.

Arduino and sensors

Links to products (not my blog posts):

Blog posts (not including ones already in the Physics Labs list):

Making PC Boards

I have about 19 posts on designing printed circuit boards, and I don’t want to try to pick out favorites.  It is probably easiest to go to posts tagged with “Printed circuit board” and start at the oldest post: Designing a printed circuit board.  Unfortunately has a strong prejudice against reading things in chronological order, and only provides reverse chronological order as an option.

Links outside my blogs:


I’m currently a professor teaching and doing research in bioinformatics (not physics or computer engineering).  Here are a few of my blog posts on bioinformatics topics:

2012 January 30

Magnetometer and accelerometer read simultaneously

In Learning to Use I2C and Magnetometer not fried, I talked about interfacing the MAG3110 magnetometer and MQA8452Q accelerometer to an Arduino.  For both, I’m using breakout boards from Sparkfun Electronics.

I  checked today that there are no problems when I connect both devices to the same I2C bus.

The first test was very simple: I put both the breakout boards into a breadboard and wired them together, then tried running each of the programs I’d written for the chips separately. Result: no problems—worked first time.

I then tried merging the programs (cleaning up any naming conflicts) so that both could be run from the same code.  After a few typo fixes, this also worked fine

I think I’m now ready to hand over the software to the students to use for their robot.

I still need to put the i2c.h, i2c.cpp, and accel_magnet code in some public place for others to use (perhaps on github? maybe on my web pages at work?) [UPDATE 2012-jan-31: I have put the libraries and the sample code for the accelerometer and magnetometer at]

One thing that is still missing is doing tilt correction for the compass heading.  Since the ROV is not expected to remain level (the accelerometer is intended to be used in a feedback loop to adjust the pitch, with anything from -90° to +90° being reasonable), getting a good compass heading requires rotating the magnetometer readings into the horizontal plane.  Only one of the students in the robotics club has had trigonometry or matrix math, so I’ll have to work with him to get him to figure out how to do the tilt correction. It may be simplest conceptually  to compute pitch and roll angles first, then rotate twice, rather than trying to do the whole tilt correction in one step (especially since the Arduino does not have matrix libraries).

2012 January 29

Magnetometer was not fried

Filed under: magnetometer,Robotics — gasstationwithoutpumps @ 13:12
Tags: , , , , ,

MAG3110 breakout board by Sparkfun

In Learning to Use I2C, I talked about the difficulty I’d been having getting the MAG3110 breakout board from Sparkfun to work, and my fears that I had burned it out by running it overnight at 5v (instead of the rated 3v).  I suspected that my problem was really a software problem, but debugging the software when I was afraid that the hardware was fried seemed like an exercise in futility.

I bought another of the breakout boards from Sparkfun (they’re only $15), and soldered on a header yesterday.  The code failed in almost exactly the same way with the new (presumed good) part as with the old (presumed fried) part, so I was convinced that the problem was indeed in the software.

I spent half of yesterday and most of this morning carefully rewriting the library of I2C interface code.  I was starting with example code from Sparkfun for the MMA8452Q accelerometer, which I had generalized to handle other I2C devices.

The library worked fine with the accelerometer, so I thought it was ok, but it did not work with the magnetometer. I added a lot of error checking (making sure that the microprocessor was in the expected state after each I2C operation), and found that things were not working as expected. The extra error checking made it much easier to diagnose the problems. I had to re-read the I2C documentation in the ATMega328 datasheet several times, to make sure that I had all the details right (I didn’t, of course). The documentation in that data sheet is better than several of the tutorials I’ve seen on line, and is both thorough and comprehensible in describing the interface.

I did not implement all features of the I2C  interface—in fact, I have a rather minimal implementation that uses polling rather than interrupts and assumes that the Arduino will always be the bus master.  Those assumptions are fine for most Arduino projects, which just use the I2C bus for talking to a handful of peripherals, but sometime in the future I may need to make a more complete set of code that can handle multiple masters, the Arduino as a slave device, and interrupts rather than polling and waiting for operations to finish.

Because I was more interested in simplicity and robustness than speed, I rewrote the code so that transactions were finished (and appropriate status checked) before functions returned.  With these changes I found that the STOP condition was not happening, despite being requested.  All other operations on the bus are checked with the TWINT bit  of the TWCR register and the upper bits of the TWSR register, but determining that STOP has completed requires checking the TSWTO bit of the TWCR register. The code I had started from just checked the TWINT bit for the other operations, and had a fixed timeout that was too short—it did no checking at all on the STOP state, just adding a fixed delay.

Once I got the STOP timing cleaned up (and earlier, making sure to send NAK when reading the last byte), everything worked fine.  The accelerometer code  had probably worked ok because there were enough delays after stops that the stops completed, even though I had not checked to make sure.  With the fixed code, even the magnetometer that I thought I had fried seems to work ok.

The interface for the students in the Robotics Club is fairly simple (much simpler than the standard “Wire” library):

// Read one register
uint8_t i2cReadRegister(uint8_t i2c_7bit_address, uint8_t address);
// Read num_to_read registers starting at address
void i2cReadRegisters(uint8_t i2c_7bit_address, uint8_t address, uint8_t num_to_read, uint8_t * dest);

// Write one register
void i2cWriteRegister(uint8_t i2c_7bit_address, uint8_t address, uint8_t data);
// Write num_to_write registers, starting at address
void i2cWriteRegisters(uint8_t i2c_7bit_address, uint8_t address, uint8_t num_to_write, const uint8_t *data);

I suppose I should check that there are no problems when I connect both devices to the same I2C bus, before handing this over to the students to use for their robot.

I should also put the i2c.h and i2c.cpp in some public place for others to use (perhaps on github? maybe on my web pages at work?). It is really a shame that does not permit code-like files as pages.

[Update 22 May 2012:  I’ve had the code available at for some time now, but forgot to update this post to mention it.]

Next Page »

%d bloggers like this: