# Microcontrollers - Beginner questions

• How do you determine how many sensors a microcontroller can use?
• My understanding is you can't use C# directly on any microcontroller but you can use C# and communicate over the USB and look for a specific port. Is this true?

In my case i've narrowed down to the three following microcontrollers:

• Arduino
• Netdurino
• AVR

This is what I'm trying to do as a first project and of course, this might not be an easy project.

I want to be able to hook up a thermometer and when the degrees get +-3 of a base temperature open or close a prop window. Also, I want to be able to store the thermometer information in a MySql Database.

So basically if the base temperature is 80, when the temperature is greater than 83 I want to push open the window. And if its 77 I want to close the window.

Im not to interested in the exact mechanism that opens and closes the windows, just how to send the signals to open or close it.

• Just a FYI, Arduino and Netdurino are development boards and not microcontrollers. Arduino is based off the AVR and Netduino is based of the STM32F microcontroller – efox29 Apr 18 '13 at 1:46
• @ChrisStratton I suspect OP is asking about actuating a physical window propped open somehow, and closed by moving the prop / actuator. – Anindo Ghosh Apr 18 '13 at 9:53
• Check out this microcontroller kit. It is wireless and has has a temperature sensor already built into it which can be sent wirelessly to a receiver. Even better, the included example code does this for you. ti.com/tool/ez430-rf2500 – Analog Arsonist Apr 18 '13 at 23:46

Microcontroller Information:

While Netduino does use C#, most other microcontrollers are programmed in C - those are two totally different programming languages. On top of that, Arduino uses a version of C++, which is different still.

Microcontrollers are typically programmed in C or in assembly (ASM). If you are new, I would recommend C. Arduino is not a microcontroller, it is a development board (usually) based around an AVR microcontroller, although there other MCUs in use. The on board MCUs (microcontroller units) will have a bootloader installed. This is a special section of code that alters how the MCU works. In the case of Arduino, it allows for programming through the serial port as opposed to standard programming methods as well as access to special library functions. Also on the board will (typically) be a USB to serial converter because many smaller chips don't have internal USB hardware, so the USB has to be converted for proper communication.

Question 1:

An MCU will have a specific number of I/O pins. These are pins which can be inputs or outputs. You can directly connect as many digital sensors as there are I/O pins, but analog sensors are limited to the number of ADC (analog to digital converter) pins available. Not all MCUs have an ADC unit. You can also connect many, many more sensors by using external GPIO expanders or parallel in, serial out registers. These can be controlled using a few MCU pins and can greatly expand the number of I/O pins available.

Question 2:

You will write a program on the computer which is installed onto the MCU. If the code allows for you to have external communications (through USB, serial, SPI, I2C, etc) then you can communicate with the chip while it is running. But that ability is dependent on the code you write. What language you use to communicate with the MCU is pretty much open to whatever you like, since the actually communication is being done by the hardware, not the programming language. Maybe that is what you meant with the mention of "C#".

Realistically, your project does not sound that complicated: read a temperature sensor (sounds like analog) and drive a motor to open/close a window based on that sensor reading. When to stop the motor will probably be based on another sensor attached to the window. This can be done with a very small MCU with few pins and little program space (such as the ATtiny24 or ATtiny25). But I think you will run into a lot of problems if you don't work on something a bit smaller first. The standard "hello world" of MCUs is to blink an LED. Then build on that by trying to control the blinking speed with a button, then drive a motor, etc.

Personal Opinion...

I do not, nor do I have any desire to ever use Arduino. I build all of my own circuitry, typically with AVR MCUs, but I understand the reason why people do use them. Personally, I think it is far better to learn how to design your own circuit and program it than to copy other people's code and piece together random things - which is what I see most Arduino users doing. There are PLENTY of online tutorials for getting started with microcontrollers that do not involve Arduino, which is what I would suggest you look into if you have any plans of designing your own circuitry in the future. If you have no desire to learn anything, then by all means, copy away, but be warned that there are often much simpler and more effective ways of doing things that you will never comprehend if you don't know what is going on.

• Storing the information may be a bit more complex, but not impossible. Communicating through USB is not easy, but can be done if you know what you are doing. Rather than try to write code for USB (it is very complicated...) I would suggest using a USB to serial converter chip and communicate through serial instead. It is much, much simpler to code and comes as standard internal hardware on quite a few microcontrollers. But like I mentioned, a USB converter chip comes on on many Arduino boards because that is how they are programmed. – Kurt E. Clothier Apr 18 '13 at 2:12
• This starts with a mistaken assumption. The presence of Netduino in the device list is a specific reference to an embedded microcontroller running C# code. – Chris Stratton Apr 18 '13 at 2:39
• Good call. I'm not familiar with that one. – Kurt E. Clothier Apr 18 '13 at 2:44
• -1 for the arduino slate. For those who are more interested in software and "maker" type stuff, like artists, etc, there is nothing at all wrong with not learning all the intricacies of MCU architectures, complex physical circuitry etc. Some hobbyists are software-centric, some hardware-, some both, some just want something to work with minimal effort/understanding. There is nothing wrong with hacking someone elses code to get something working fast, even if you don't understand it all. – Brad Apr 18 '13 at 19:11
• @Brad: I never said there was something wrong with it. I understand that some people don't care about hardware, and that is totally fine. I simply stated that if someone had any interested in designing circuitry (HARDWARE) in the future, it would be worth the effort to start with the bare chip instead of a development board. Also, the "intricacies of MCU architecture" has nothing to do with it. I don't care what is happening in the silicon, but unless you look at the datasheet and learn a little bit for yourself you'll have no idea what a chip can do, only what other people have done with it. – Kurt E. Clothier Apr 18 '13 at 20:41

### The choice between the platforms

• Arduino is an open source platform built (mainly) around on AVR (ATmega) chips, with one exception: the Arduino Due is built around as Cortex-M3 CPU. It is programmed in C, C++.
• Netduino is a platform built around Cortex-M3 and Cortex-M4 chips. It is programmed in C#
• AVR is a family of microcontrollers by Atmel, programmed in C, C++, or Assembly. The microprocessors are at the heart of most (but not quite all) Arduinos.

Which one you choose is a matter of personal taste. AVR is more low level thatn Arduino, so if you are just starting, Arduino is definitely easier to get started with quickly. Netduino is there is you really want C#, however, Arduino is better supported than Netduino.

### Interfacing to the PC serial/USB port

On the PC side, no matter what you choose, you will be able to interface to your Arduino/Netduino/bare AVR chip/circuit via serial port/USB. On the PC side you can use whatever programming language you desire as long as it gives you access to serial ports. The way this will works is that Arduino/Netduino/bare AVR will read/write from serial/USB port and so will your PC program. Communication ensues.

On the embedded side, most of the things you will consider will have a USART, meaning serial communication are set. However, man pure AVR chips do not have a USB port, per se out of the box: you'll have to build a USB support via a separate circuit, though as Kurt E Clothier points out some AVR chips have built-in USB support. A few AVR chips don't have a USART built-in, but you are not likely to pick those.

### Temperature sensing

As far as temperature sensing, all of the platforms you suggested will give you the capabilities for doing that. Reading a temperature can be as simple as reading an ADC if you are using LM34 or an LM35 temperature sensor. On Arduino this is accomplished with one line: myReading = analogRead(pinNumber); You will have to transform the temperature reading into actual temperature, but this is not particularly difficult.

### Motors

The motors a bit more complicated: for most (almost all) motors your Arduino/Netduino/AVR chip will not be able to drive the motor directly. You will need at least a MOSFET/BJT switch (see here for more info) and a snubber diode, or, more robustly a motor driver circuit. The Arduino platform has a large variety of peripherals (in general called shields, though Olin prefers the term daughterboard) including a motor shield. If you are using a high-power motor this is the way to go.

• Nice explanation, although I would disagree with your thoughts about Arduino definitely being easier. I firmly believe that taking the extra bit of time up front to learn what is going on will help you tremendously in the future. Using Arduino severely limits the design possibilities. But then again, that is just my opinion - no more valid than yours. One other thing, there are some AVR chips that have built in USB, for example: hackaday.com/2011/08/07/1-chip-usb-avr-development – Kurt E. Clothier Apr 18 '13 at 6:13
• Why is there an analogRead if digitalRead does the same job? – jippie Apr 18 '13 at 6:19
• @KurtE.Clothier: Thanks for that link. Wording on getting started ajusted: what do you think? – angelatlarge Apr 18 '13 at 6:21
• @jippie: You are, of course, correct sir. That was a brain-o (like typo, but when brain malfunctions) – angelatlarge Apr 18 '13 at 6:21
• Looks good to me. – Kurt E. Clothier Apr 18 '13 at 7:48

The Netduino runs a version of the .NET runtime known as the "micro runtime" IIRC. You write code in C#, and it gets interpreted on the Netduino. The netduino ships with libraries to access the various functions of the microcontroller, and those libraries are written in C. The C# code you write will be very slow to execute, though the library code is fast. I write C# code for a living, but I don't think the netduino is a great choice for a microcontroller; it's harder to use than an arduino in some ways, has far less support (less example code, less compatible hardware), and costs more. I also don't think C# is the right language for controllers.

I've done projects with custom-written code for AVRs, and other projects with Arduinos. If you are just starting out the arduino has tons of advantages, and the only disadvantage is the higher cost than a raw AVR.

I recommend looking at some of the introductions for the AVR and for the arduino. I think that will give you a good idea which one is better for you.

How do you determine how many sensors a microcontroller can use

That's would depend on how creative you can be and how many iopins you have available, and how many pins the sensor requires. If you have 4 3pin sensors, that doesn't necessarily mean you need 12 pins to control them, you can multiplex them and use 5 pins and save 7 io pins. So it all really depends. But one way of looking at it is, how many available io pins do you have, and how many pins do your sensors requires, that would give you a low limit on how many you can have. Creativity, experience, and understanding of how everything works, can increase the number of sensors.

My understand is you cant embed C# directly on any microcontroller but you can use C# and communicate over the USB and look for a specific port is this true?

While I haven't done much USB, I do know that if you are doing USB from scratch, that would involve writing drivers on your host machine. The more popular way is to use an IC that already handles all the USB protocol and the drivers and it behaves like a serial port on your computer. You basically have a USB-Serial bridge which can tie into a serial port on your microcontroller. I believe the Arduino does this. Not sure about Netduino. So yes you can use C# to communicate over USB THROUGH a specific port (assuming you are doing it the easy way and using a USB-Serial bridge).

What you propose to do is definitely possible. You can write a program that interfaces to your database and that can send and receive serial data to your microcontroller.

• thanks for the answer. I can see on sparkfun they have the controllers and the thermometer, but what type of motors work eith a microcontroller? – user2278106 Apr 18 '13 at 2:07
• Any motor really. Each motor has to be controlled, and depending on the motor, that can vary between models, types, etc.. Once you know how the motor is controlled, you would have to create a circuit ( a microcontroller alone is not good enough), that interfaces your microcontroller to the motor. – efox29 Apr 18 '13 at 2:12
• @user2278106 Motors require higher current than a typical micro can provide. You'll most likely need a motor driver or some similar solution (say, an H-bridge). Motor selection is dictated by the mechanical requirements of the system (how much torque is required to open/close the window, and how fast do you want it to open/close), and drivers are chosen/designed once a motor is selected – helloworld922 Apr 18 '13 at 2:13

To answer a specific point, a microcontroller could presumingly have a (very high number) of sensors attached. Using bus sensors, like i2c, spi, one wire, serial, or any other bus protocols, would allow you to have x many per bus, where x is the limit for a given bus. You would have to follow some technical specifications for wiring the bus, and with multiple of the same bus, could expand even more.

The limit is really in terms of speed and practicality. I could have a thousand sensors on a single arduino, but could I power, read, process, and display that information in a reasonable amount of time? Maybe, depends on how well you can code or how imaginative you get.

Even analog sensors can be done like this, using multiplexers or ADC ics. The better question isn't "how many sensors a microcontroller can use" but "how many sensors do I need" and take it from there.