Author Options:

AVR, Variable resistors, USB Answered

Hello everyone,

What a great site this is, I've only seen like 50 projects so far and I know there are an incredible large amount still left to discover on the site.. for all of you who write the guides and articles, great job! =)

So on to my questions..
I'm trying to learn how to build and program a circuit with an AVR uC. Previously I've played with NE555 circuits and I've connected those normal HD44780 LCDs to the LPT port and so on..

I've also worked with repairing TV sets earlier, replacing both normal components and SMBs..

So I got some basic knowledge of soldering and reading schematics, so thats no problem.

However the programming part is more problematic and also how to connect some components to the uC.. My wish to learn is how to connect variable resistors to the uC and connect the uC to a computer (via USB perhaps) and then read the values there and perhaps sending them further to another program.

More simple, I want to be able to connect a variable resistor to a uC and then be able to read the value and use that value on the computer..

In the long run, what I want to accomplish is a similar piece like the Aurora open source mixer. http://www.auroramixer.com/

But first I want to learn how to connect lets say 2-3 variable resistors, which I believe should be connected to the ADC channels on the uC, right? Together with a low value resistor between the variable resistor and the uC, I've got that info from somewhere so I hope I'm on the right track.

Ok, so that's quite simple, I guess.. But then, how do I read the values on the uC? I think the uC should poll the ADC pins with a few milliseconds in between, or does it work in some other way? Before I connect it to a PC, a simple way to see if it works would be to send the "value" from the variable resistors to a led for each resistor and increasing or lowering the brightness of the led depending on the value from the resistor.

That should be quite simple to rewrite when hooking up the uC to a computer, just forward the info the PC instead of to the led.. or to them both perhaps..

What would the code be for doing such?

My "problem" as most people think, is that I learn by examples, not by books or pages on the net with advanced explanations.. I need to have it quite simple, even though I have no problem learning new stuff..

I've been working with PHP programming for a few years and even though I know its far from PHP in a uC, its still programming so I have some sort of basic thinking, which I hope will be handy somehow =)

I've searched through the net, also searched AVR Freaks and I've even asked on the forums of AVR Freaks who tells me, sure you can do that, you just have to start programming.. But I really dont know where to start..

I learned PHP by downloading examples, see what they do, change a bit and see what happens and so on.. Sure I can do that with the AVR as well, but there are no examples that even read just one pin with a variable resistor connected.. if I had that I would be able to duplicate and make it read on more pins and so on..

I should be able to emulate the code somehow with the AVR developer software, so somehow I should probably be able to emulate a variable resistor and find out what values I get from it.. but I havent been able to get something like that working either..

Finally, I'm now writing here cause this page hosts a great load of great guides, from "how to sew your own panties" to "building your own waterbottle rocket".. and because I didnt get that much help from AVR Freaks..

so you guys are my last hope, I hope you could help me out..

// Chris

ps. if someone would be interested in making a guide on how to accomplish this and perhaps even connect the uC over USB to a computer, that would be the best thing ever ;) but I'm not gonna go that far, just some help would be enough.. hehe .ds


The forums are retiring in 2021 and are now closed for new topics and comments.

11 years ago

Based on your post, you should easily be able to accomplish this.

Reading a POT

--Yep, your instincts are correct--use the ADC channels to read voltage change.

The wiring is simple--if the AVR is powered by 5V, just connect one end of the POT to VCC, and the other end to GND. This forms a voltage divider, and the center wiper pin of the POT will range between 0V and 5V.

Use linear POTs, of course. Anything 10K to 500K would work fine.

-- ADC code

Read the tutorials on AVRFreaks, all the webpages you can find AND the datasheet for your AVR.

NOTE:All the upper case labels are defined constants. AVRgcc uses all the same naming schema as the Atmel datasheets..

Here's how I've done it in the past:
// single ADC readvoid getADC(uint8_t pin){	ADMUX = pin;				// Select pin using MUX	ADCSRA = _BV(ADEN) | _BV(ADPS2);	// Activate with Prescaler 16                                        // 4Mhz/16 = 250kHz read	ADCSRA |= _BV(ADSC);			// Start sampling	while (ADCSRA & _BV(ADSC) ) {}		// wait}

1--Set the ADC pin.
2--Set the speed of the read. This must be tailored to the current CPU speed. In this case, I've set the read a little too fast for the full 10-bit resolution, but I was sacrificing accuracy for speed.
3--Initiate the read.
4--Wait for it to finish.

Then call the function with the ADC channel number you want (different channel = different pin.)

NOTE:the ADC read function does not return the read value, simply because that's more memory intensive.
// Read ADC channel 1getADC(1);adcval = ADCW;

The ADCW is the defined name of the ADC return value register (full word length.) Of course, this could be setup as a return value for the function, As I noted above.

Getting that info into the computer

Usually done with a serial interface (RS-232 or possibly I2C.)

Unfortunately, "bare" AVRs don't support USB (although there are hacks to do so in a limited way.) A separate support chip is often needed.

Arduinos, however, DO support USB (they are AVRs together with the support chip.) In fact, it's probably easier to read the ADC pins on the Arduinos, too.