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.

Everything Else » Multitasking ADC input pins

March 05, 2011
by amartinez
amartinez's Avatar

Unless I'm missing something, I have not found anything in this forum on multitasking so I thought I'd ask this question.

I am looking to symulataneously measure 2 variable voltages less than 3V from 2 separate ADC pins. The sample rates on each input pin could be as low as 100 Hz or even less, however, I also need the chip to show two separate LED indicators relative to the voltage levels detected. What I'm going after is basically 2 simple volt meters who work symultaneously off the same chip. Is this possible? Am I asking the chip to do too much?



March 05, 2011
by Ralphxyz
Ralphxyz's Avatar

On a single core processor you can only do one thing at a time, though those things are done so quickly they might as well and often are considered simultaneous i.e. multitasking, be that as it may this might help get you started it is a thread on using multisensors (including my multi sensor code) with a good explanation by Mike on what is possible.

So you get your "simultaneous" reading of two variable voltages from 2 separate ADC pins (my code does 4 or 5). Using PWM you "should" be able show two separate LED indicators or you might need a DAC (Digital to Analog Converter) for the LED indicators. Possible the ATmega mcu will do it all for you, I am not sure, but getting the two voltages definitely will work.


March 07, 2011
by amartinez
amartinez's Avatar


Thank you for your help but I was amiss in my wording. The code would probably be very simple in comparison as the LED indicators I was referring to were simple LED lights. I should have been more specific. No LCD needed for this project.

2 rows of 3 LED lights each. One row of LED lights would represent a voltage range from PC0 and one row from PC1. For example, if PC1 reads a voltage of 1 to 2V, then the first LED goes on. As the voltage increases more LED light up in the row light. It's a simple way to visually monitor two separate voltage inputs. Kind of like Humberto's LED sound meter but much simpler.

One question. Is the code you posted the full code? I'm not very astute in C but I think there are variables missing and a main as well. I added the libraries and an int main statement, I ran the code through and I get the following message from the compiler. I indented the DOS compiler output as codeblock to reduce word wrapping.

make -C ../libnerdkits
make[1]: Entering directory `C:/C_CODE_PROGRAMS/libnerdkits'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `C:/C_CODE_PROGRAMS/libnerdkits'
avr-gcc -g -Os -Wall -mmcu=atmega168  -Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscan
f -lscanf_flt -lm -o sensor.o sensor.c ../libnerdkits/delay.o ../libnerdkits/lcd
.o ../libnerdkits/uart.o
sensor.c: In function 'main':
sensor.c:34: warning: unused variable 'difference'
sensor.c:33: warning: unused variable 'sensor5'
sensor.c:32: warning: unused variable 'sensor4'
sensor.c:31: warning: unused variable 'sensor3'
sensor.c:30: warning: unused variable 'sensor2'
sensor.c:29: warning: unused variable 'sensor1'
sensor.c:28: warning: unused variable 'sensor0'
sensor.c: At top level:
sensor.c:44: error: expected identifier or '(' before 'while'
sensor.c:124: error: expected identifier or '(' before 'return'
sensor.c:125: error: expected identifier or '(' before '}' token
make: *** [sensor.hex] Error 1


Simply put, I'm just trying to light 3 LED lights relative to voltage input.

Here my addition to the code before yours

#define F_CPU 14745600

#include <stdio.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

int main(){

    // PC0 -- temperature sensor analog input
    // PC1 -- temperature sensor analog input
    // PC2 -- temperature sensor analog input
    // PC3 -- temperature sensor analog input
    // PC4 -- temperature sensor analog input
    // PC5 -- temperature sensor analog input

//*    DECIMAL Output
  uint16_t sensor0 = 0; 
  uint16_t sensor1 = 0; 
  uint16_t sensor2 = 0; 
  uint16_t sensor3 = 0; 
  uint16_t sensor4 = 0; 
  uint16_t sensor5 = 0;
  int16_t difference = 0;


Thank you for your time, looking forward to any ideas.


March 07, 2011
by Ralphxyz
Ralphxyz's Avatar

amartinez, You are correct, the code I posted was just the modification to the tempsensor project code enabling multisensors.

It just fits in place of the equivalent tempsenor code. It just replaces the while(1) loop I believe (I have not looked at it recently).

That with the the additional variable declaration should give you working multi-sensor code.

Essentially the Switch(mux) statement just literally switches between mux settings enabling multiple pins for ADC input.

You could probable use a if > < statement to sense the voltage and light the appropriate led.


Post a Reply

Please log in to post a reply.

Did you know that two resistors plus a transistor can be used to make a better voltage supply? Learn more...