NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Project Help and Ideas » Troubles connecting gyro to ATmega168
April 12, 2011 by MeekMoe |
Hello, I am fairly new to nerd kits and I am currently working on a project where I am connecting a 2 axis gyro to my ATmega168. The gyro I bought (http://www.pololu.com/catalog/product/1266) came on a breakout board and seemed relatively easy to hook up. However when I try to get reading off of it on the LCD i get nothing. It starts up reading all zeros and if i try moving it around nothing happens to the zeros. sometimes there is junk printed out on lines i am not even printing to. Is that a power issue? I thought that it might, like in the case with a servo, so i powered the gyro using the usb cable (worked for the the servo). Behavior was unchanged. Another thing i did which i am not sure i should have but also not sure it matters. I miss read the website and used the 3.3V on the gyro and connected it to BOTH Vrefs (on breakout board and on microcontroller). I also connected both ground pins on the breakout board. I was using code similar to the temperature sensor code but i seems like i am missing something... If anyone has any input it would be greatly appreciated. Ill comment code and post it soon. MeekMoe |
---|---|
April 12, 2011 by MeekMoe |
ok here is my code. hope it amuses some.
|
April 12, 2011 by 6ofhalfdozen |
Hi MeekMoe, Any chance you could post a photo of your wiring? I took a look at the link for the gyro, and it sounds like the wiring needs to be done JUST right. There are two things about your description of your wiring that stand out; power, and grounds. From the way I read the silkscreens and website, if you feed it with 5V (NK or USB), you put power in on Vin pin and do not connect the Vdd/3.3V since the board automatically drops 5V to 3.3V.
The website mentions that the powerdown and selftest should never be held high at 5V, and sounds like tieing them to ground it best if you aren't using them. Also, it mentions that with no rotation, it should output 1.23V. Since you get no reading what so ever, I am thinking it is a wiring problem first then maybe a code issue. You just posted your code, so I haven't read that yet. my suggestion is check your wiring. It looks like a cool gyro, hopefully you can get it working with just a minor tweak. |
April 12, 2011 by bretm |
"sometimes there is junk printed out on lines i am not even printing to." It looks like you're printing to all 4 lines, so what does the junk look like and on which lines/columns does it appear? The adc_read() routine starts a new conversion before it returns the value from the previous conversion. So when you do this:
it will start another ADC0 conversion before giving you the value back. So when later you do this
it may still be in the middle of that conversion. That means that when you later do
you're actually reading the ADC0 value that it was still working on. adc_read then starts another conversion (using ADMUX=1 which you just set) so that when you loop around again you're going to be getting the ADC1 value back when you set Xaxis = adc_read(). Does that make sense? But that doesn't help explain why you're getting zeros. Remove the ADC and LCD stuff from the picture first. Just attach a voltmeter to the gyro outputs. What voltage range does it give you when you move the gyro around? |
April 12, 2011 by MeekMoe |
On the left you can see that there are two GND. I connected them both. in the side view red is Vin, yellow 3.3V to refs, Blue is signals. So i tested out the pins on the breakout board and now im convinced its a code issue. Everything was working on breakout board,3.3V and ~1.23V on signals out. The junk printed out is u :"2.222"" As far as interrupting the ADC, should i just ad a Delay before ADMUX = 0,1 to allow time for the adc to convert? also i was wondering do i need to define PC0 and PC1 as in input like i did with the LED as an output? |
April 12, 2011 by MeekMoe |
OOOOOO it just clicked in my mind. i now get what you mean about the reading of the ADC values and thats because the conversions get started at the end of ADC_read function right?? Ill adapt code and see if it changes anything. but your right, it still doesnt explain the zeros. |
April 13, 2011 by MeekMoe |
Ok so i figured it out, silly me, i forgot to adc_init in the main function silly me. After i got that figured out it has started to work. thank you both for you quick response, they were both helpful. But now i have another question as far as my project goes. Is there a way to get time stamps or find out how long processes take? I want to integrate the gyro over time in order to track the rough orientation. Really all i want to do is make the gyro recognize when its nearly turned over, or at least past 90 degrees. Anyone have any advice? i think there are time stamps in C but it requires the time.h library.. MeekMoe |
April 13, 2011 by Ralphxyz |
Hi MeekMoe, please post a project outline and your working code. Lurking in the back of my mind is a balance bot project that will need gyros. In order to do a time stamp you will need a time reference. I think the time.h library references a pc's clock you will need to furnish the clock. Of course you could reference a mcu timer and show an elapse time. There is a Nerdkits Crystal RTC project and then there is Rick's great I2C Real Time Clock project. This adds a bit of complications beyond just calling a library function, but hey. Ralph |
April 13, 2011 by 6ofhalfdozen |
MeekMoe, Glad to hear you got it working. As for finding out how long processes take, if you are talking about how long it takes to run X lines of mcu code, there are several threads on here talking about that. If you are talking of macroscopic timing for data monitoring, ie seconds/minutes/etc, there are several threads on that as well, usually based around the real time clocks. TimeDate stamps can be a tricky one. I am definately planning on putting a couple in my future projects, sadly haven't gotten there yet. The simplest one I can think of is to have a timer interupt set for a certain number and use it to accumulate a variable off of that..(like in the real time clock project, but instead of seconds have it accumulate into a 16bit 20 second variable or something...) then have the accumulator stop once the NK dumps the data off elsewhere with a timestamp and back calculate it on a pc or something. Simple to me, not so simple to others.. just a thought.. Ralph, Rick, Noter, and others who's names I can't recall at the moment, have been fighting with external real time clocks in another post or two or three. Ideally external RTC's are probably the best way to do a "real" timestamp without bogging down your NK mcu, but it seems to be a bit more complicated than one would think. Sadly, just trying to keep up with their marathon posts has gotten me pretty confused. So I will defer to the experts on that topic. As for finding out if your gyro has gone over 90degrees sounds pretty simple to me, if you are looking for the x and y axis seperately. If I recall, the gyro outputs + and - values for the X and Y axis changes in the gyro. If you were to set a signed integer variable, call it Xaxis and Yaxis just add it to the ADC reading every so often. You would need to zero the variable at a "level" or zero angle for each one. Once you have it zero'd, it should add and subtract(ie add a negative value), as changes occur. Noise might be an issue, so you might need to do some filtering. Anyhow, set an if then in your code so that if Xaxis or Yaxis goes over the ADC value for 90degree and LED or something goes on/off. I think once you have the gyro talking to the NK, the hard part is mostly over for this task.. anyhow, hope that helps a little. |
April 13, 2011 by MeekMoe |
My project goal it to create a reliable parachute for my pressurized water bottle rocket. I can make the rockets over stable such that at the apex of flight, the rocket will turn to fall back down to the ground. It is at this point I want the gyro to trigger the servo to deploy the parachute. Accelerometers don't work because the rocket is in free fall while it is still traveling upward with great velocity. here is my most recent working code. As you can see I Have tried to "force" a time stamp in order to try to do the integration process. I was hoping that if i made a delay that was larger than the mcu data collection processing time, yet small enough to integrate, that a accurate integration would be possible, but its not looking like it. Ill try and read up on those time threads you guys mention and see if i can get something better working. Please let me know if yo have questions or comments or advice about the code.
|
April 13, 2011 by hevans (NerdKits Staff) |
Hi all, Just to throw another option for the "timing" question. If what you want is to know the interval between to successive readings of the ADC you can probably get a fairly accurate time by just knowing how long it takes for you to get a new sample. If you take a look at the datasheet you will see it takes 13 ADC clock cycles for you to get a new sample from the ADC, and you know each ADC clock cycle takes 128 MCU clock cycles (assuming you are using a 1/128 prescaler). So assuming you structure your code so you pick up each new conversion as soon as it is ready, and start the new one immediately, you should able to have a very consistent time between collected samples. This approach might certainly be good enough as an easy to implement first pass which will not require you to have any external circuitry to keep track of time. Humberto |
April 14, 2011 by MeekMoe |
I like that idea, but that leads me to ask... If I switch to ADMUX = 1 before i read from the ADC, will the ADC update or contain its previous value from ADMUX = 0? Also I am assuming the ADC runs parallel to the main code? |
April 14, 2011 by bretm |
Once you read the value from the ADC data register there is no guarantee that it will retain that value after that point, whether you start another conversion or not. Once you read ADCH, technically the MCU is allowed to update the ADC data register at any point after that. I don't think it does unless you do a new conversion, but you can't count on that. Nor do you need to--you'll keep your own copy of the value to use as long as you like. Yes, it runs in parallel to the main code. |
Please log in to post a reply.
Did you know that you can make a capacitive proximity sensor with some aluminum foil and paperclips? Learn more...
|