NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Reading more than one Temp

January 28, 2011
by billwis
billwis's Avatar

I am new to NerdKits and C programming but not electronics. I have run the tempsensor project and did some minor code changes that worked. My problem is getting more than one sensor to work. I have tried all the suggestions on the forum and get nowhere. I only need to get that little light in my head to go on with this C language. Is there a sample from start to finish that I could use. I did some Basic programming with the PIC years ago and it was simple.

Thanks BillWis

January 28, 2011
by Ralphxyz
Ralphxyz's Avatar

Hi BillWis, welcome, had you searched the Nerdkit forum for "multi sensor"?

You would even find my multi sensor code, which might not be very elegant but does work.

This is just modified tempsensor project code.

Ralph

January 29, 2011
by billwis
billwis's Avatar

Thanks for getting back so fast. I have tried many different examples but have had no luck getting things to run. Would you have the code for adc1 and adc2 I could copy and work with? I have no idea what I am doing wrong.

bill

January 29, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi billwis,

The code that Mike wrote in the post that Ralph linked you too is pretty much as bare bones as you can get for reading two sensors. The trick is to understand that the ADC input is multiplexed across the six different possible inputs. You can only read from one at a time and the one you are reading from is determined by the ADMUX register. If ADMUX is set to 0, then the ADC will read off the ADC0 pin (see page two of the datasheet. If ADMUX is set to 1, then the ADC will read off the ADC1 pin. I think with that you should be able to modify the code to read off the pins you want. If you run into trouble feel free to post your code and there are plenty of people here that would be happy to help.

Humberto

February 09, 2011
by billwis
billwis's Avatar

OK I have been trying to get two seperate LM34 sensors to read and display on the LCD. My problem is figuring out how to select the different ADC channel ( I understand the multiplexing ). I can read them individually but can not get them to read together. I have tried the code in many different ways, searched the internet and found many different ways to try but nothing will work. I get errors when I try to upload to the MC. Do I have to select the ADC channels by themselves or is there a for / next loop like in BASIC that can be used to select the channel? Also do I have to write the conversion code for the individual channels or can I make a call to it some how? It should not be this hard but I am just stumped!

Thanks for the help Bill

February 09, 2011
by Ralphxyz
Ralphxyz's Avatar

What in my referenced link does not answer your questions? I included my working multi sensor code at the end.

Now which is your problem? Code that does not work or problems uploading to mcu?

As far selecting the ADC channels (mux) this "switch/case" statement steps through 6 sensors.

The syntax is very similar to what you would use in BASIC :

   Select [ Case ] testexpression
       [ Case expressionlist
            [ statements ] ]
       [ Case Else
            [ elsestatements ] ]
    End Select

for(mux=0; mux<=5; mux++)
    {
        ADMUX=mux;
        temp_avg = 0;    // re-set temp_avg to 0 for each new ADMUX value!

                for(i=0; i<100; i++) 
                {
                    last_sample = adc_read();
                    this_temp = sampleToFahrenheit(last_sample);

                    // add this contribution to the average
                    temp_avg = temp_avg + this_temp/100.0;

                    switch(mux)
                    {
                        case 0:
                            sensor0 = temp_avg;
                            break;
                        case 1:
                            sensor1 = temp_avg;
                            break;
                        case 2:
                            sensor2 = temp_avg;
                            break;
                        case 3:
                            sensor3 = temp_avg;
                            break;
                        case 4:
                            sensor4 = temp_avg;
                            break;
                        case 5:
                            sensor5 = temp_avg;
                            break;
                    }       
                    //delay_ms(1000);

                }

        }

re: "It should not be this hard but I am just stumped!"

Just slow down and take it step by step it will start to fit together and work for you.

Try to narrow your questions down to being more specific, as mongo has said in other threads make a flow chart of your code (great advice). Some times if you have a visual representation of your code errors in your logic or implementation become very clear.

And of course ask often.

Ralp

March 06, 2011
by billwis
billwis's Avatar

It has taken a while for me to get back to the kit. As I was searching the net for my answers I came across Ralphs multiple ADC program. What an eye opener. I was real close with my code but had the lines in the wrong place. After going through the code I found my problem and modified it to do what I wanted. Thank you all for the help. Nothing left but to control the world.

Thanks BillWis

Post a Reply

Please log in to post a reply.

Did you know that our customers love us? Learn more...