Microcontroller electronics

Hi, I can remember when I was doing physics at A level, and when you wanted to measure say the temperature of a room, with a thermistor, you would be it into a circuit, with a static resistor in series, then measure the voltage across the static one, which you could then work out the resistance across the thermistor, and hence the temperature based on an already calibrated conversion, but with a microcontroller ( I am using orangutan mega8), how do you use sensors, because you cant measure the voltage across the component, and send that to the microcontroller, is there a function of the microcntroller for doing this.

I understand I use the inputs on the board, but cant just stick a sensor in there, I have to build a small circuit around it… Does anyone know of an example where something similar is happening?
Thanks
Alex

You can do basically the same thing with your Orangutan. The ATMega8 MCU has a 10-bit analog-to-digital converter (ADC). Basically it’s a piece of hardware that can convert a reference voltage (which must be between ground and your operating voltage, 5V in this case) to a ten bit number. There is only one converter, but it can be switched to be connected to one of eight different pins. Any of the six I/O pins of PortC can be connected to the ADC, and there are two dedicated ADC pins ADC6 and ADC7, both of which are brought out to the Orangutan’s headers. On the Orangutan, ADC7 also has a 10K potentiometer (i.e. variable resistor) connected to ADC7 for testing.

So, you could connect +5V to a thermistor to a fixed resistor to ground, and also run a wire from the thermistor/resistor node to your analog input pin. As the temperature causes the resistance of the thermistor to vary, the voltage between the thermistor/resistor node and ground changes (the same thing in this case as the voltage drop across the resistor, but the ADC only measures voltages relative to ground), and that voltage is converted to a ten-bit number stored in a two-byte register in your ATMega8. For example, if the reference voltage was 2.7V: 2^10*2.7V/5V=552.96
So the ADC register would read 552 (since the processor truncates, rather than rounding).

You might want to download the Orangutan-Lib examples. You might want to look at all of them (they’re nicely written and prolifically commented), but I’m specifically thinking of the “analog-only” example project. It shows how to set up the ADC on the ADC7 pin, and to turn the red LED on above a specific voltage by turning the potentiometer. Since ADC7 is also brought out to the Orangutan’s header, you could also hook up a volt-meter between ADC7 and ground to look the voltage as you turn the potentiometer. Fun!

You can also interface a wide variety of digital sensors to your Orangutan. This is a little harder to do when you’re just beginning, but opens up a possibilities in terms of the type and number of sensors you can use. There are some nice, low cost digital temperature sensors like this one. They don’t come out and say it, but this guy is probably just a thermistor/resistor and a very basic microcontroller with a single ADC in a small package!

-Adam

Thanks Adam, very detailed reply.

So in terms of wanting to learn to read from sensors, what do you reckon i should buy. Should I also get a bread board to build the circuits on, to make it easier. Is there anything else you would recommend??

Breadboards are excellent, I would definitely recommend getting one, or a few, and a nice big jumper wire kit (the little basic one is never enough). The female headers on the Orangutan are great for connecting to a breadboard with just jumper wires.

Not to sound like a broken record, but the book I mentioned in the other thread covers a lot of breadboard experiments interfacing analog and simple digital sensors with an AVR, and it’s available as a kit with all the necessary components. You don’t even necessarily need to buy the kit (for example, the Orangutan already has a built-in potentiometer), but the parts list is there on the website, so you could pick up bits as you decide which experiments you want to do yourself, and which you understand well from reading about how they would be set up.

I promise I don’t work for the publisher. I actually heard about “C Programming for Microcontrollers” on this forum, and it really helped me get up to speed when I was beginning to try to do some serious stuff with my Orangutan (after weaning myself off of PIC microcontrollers) and struggling with the kinds of questions you’re coming up with now. Actually I just looked up the first message I sent to Tom Benedict asking about the book, which he had mentioned in this thread, and it was exactly one year ago today. Wow. Thanks Tom!

-Adam

Yeah sorry i didnt mention, i bought the ebook of it. I didnt want to buy the kit because it came with another microcontroller, and i thought that as i had one i would like to learn that first…

Sweet! I had forgotten it was available as an e-book. Do they let you download it or mail you a CD?

The kit is pretty common stuff, I think you can get most any of the bits from Radio Shack, and you can get some nicer breadboards here from Pololu. I like the big 1660-point breadboard, especially since it has two power rails in the middle, so you can pack it really full. Something smaller, like the 830-point, or even the 400-point breadboard would be plenty for the experiments in the book. I like to keep a bunch of the little 400-point boards around with circuits I use a lot on them, they link up like legos!

-Adam

Yeah thats a good idea, about keeping useful circuits at hand. You download it, its pretty useful. I cant buy from pololu because I’m in the UK, so im trying to get equivelents from rapid electronics, but there breadboard selection seems a bit poor. The maplin store has this: what do you reckon: maplin.co.uk/Module.aspx?Mod … t&doy=18m2

It has no sensors though, but a good selection of other bits (not all i’m sure how to use…)

Anyway, I’ll keep looking, and when i get stuck i’ll badger you :slight_smile:

That looks like a very nice starter kit, although you’ll need more LEDs if you want to build the Cylon Eye experiment (Joe Pardue is a big fan of the original Battle Star Galactica apparently). Of course you’ll need an actual soderless breadboard to go with it, but it looks like this store has lots to choose from (it’s hard to see from the picture, but I think the included strip-board is something you’ll have to solder components to, so it wouldn’t be great for prototyping).

Also, you might want to pick up a couple of these IR LED and photo-transistor pairs to do the optical switch experiments in the book. You could also throw in a TIP115 power transistor and a small motor for the motor speed control experiment. I would probably skip the opto-isolator experiments.

-Adam

Whats the deal with relay’s. If i want to control a normal desklamp - and turn it on at certain times of the day, what relay would i need, they have a lot of jaron with them… How does it work. do you scre each end of the wire in to terminals, and it completes the circuit inside?

There are tonnes of different kinds of relays, but in the end they’re all just some sort of relatively high power rated mechanical switch that is electronically (usually electromagnetically) actuated by a lower power source. In a good relay, the high and low power sources are completely physically separated. Imagine gluing a small but powerful magnet to a wall switch, then putting an electromagnet above, but not touching it. Applying voltage to the electromagnet in one direction would attract the switch, flipping it one way, and applying voltage in the other direction would repel it and flip it the other way. You could use a small battery to power the electromagnet, while keeping yourself safe from the mains voltage.

This is an example of a latching relay, which basically means it stays where you put it. Most relays are non-latching, which is a little cheaper and easier to control. Picture the same light switch, but this time you’ve glued a piece of iron to it. Now it will be attracted to the electromagnet no matter which way you hook up the voltage, so lets also add a rubber band pulling it down away from the magnet. Most of the time the lights are off, but when you power the electromagnet it pulls against the rubber band, turning the lights on.

Most relays are basically that, a set of metal switch contacts spring-loaded in the open position, but which can be pulled closed by an internal magnet or solenoid. To turn on a desk lamp you would need a SPST (or more poles/throws, are you familiar with switch terminology?) relay with a current rating at 240 volts AC (in the UK anyway) high enough to handle the current drawn by your lamp. You would wire it in-line with the lamp’s live wire, and connect something like a microcontroller to the input lines. Depending on the relay, it may take a fair amount of power from your input device to switch. Your basic AVR digital output can source 40mA at 5V, so you may need to put a transistor (or a smaller relay) in between your digital output and your solenoid.

Of course, if all you want to do is switch on a light at a particular time every day you can just one of those spring-load rotary appliance timers, but what fun is that?

-Adam

Unless your being sarcastic, I agree. I have seen those plugs you plug the lamp into then, plug that inoto the wall socket, click the little things to tel it when to come on - BORING!, no i want my microcontroller set up to do it! Thanks…