Part III of Build Electronics - Interfacing With a Microcontroller

in #robotics7 years ago

Hello everyone, sorry its been a while since my last post - winter break is over and school is back, so I’m trying to post whenever I can. Today we are going to talk about a more practical use of what we have learned so far: using a microcontroller to interface with electronics. Once you learn how to do this - how to program and effectively use a microcontroller - you can begin to design more complex circuits.

I think the first thing that needs to be talked about is what is a microcontroller? A microcontroller is a device with pins or ports in it, called GPIO pins, that you can designate as either input or output (more on this later), and use these to control and interface with electronics. Microcontrollers can be programmed to carry out certain tasks. Some common examples of a microcontroller would be the Arduino and the Raspberry Pi. Arduino program is written in the C language, while the Raspberry Pi is written in Python. You dont have to know everything about these two languages to write code for their devices, just memorizing simple functions and the syntax should be enough to get started (But as you build more complex things more of the language will have to be learned).

Now lets talk a little about how microcontrollers interface with different electronics. Microcontrollers have different pins called GPIO pins. They can either receive an electric current or emit one (Input and Output). In your code, you can designate whether your pin is input or output like this:
Arduino -

If you want input instead, just type ‘INPUT’ instead of ‘OUTPUT’.

Raspberry Pi -

In this case, ‘GPIO.setup(7,GPIO.OUT)’ Designates pin 7 on your Rpi as Output. If you would want input, instead type ‘GPIO.setup(7,GPIO.IN)’.
(Ignore the rest of the code for now, I was just using it as an example.)

If your pin is an output pin it can output an electric current. This is very useful for controlling motors, LEDs, and anything else that needs an electronic output.

If your pin is an input pin, it can receive an electric current. Now this may not sound like it can be useful, but it is very useful, almost necessary. There are two types of input pins, analog and digital. Which one you will use really depends on you purposes (Spoiler: digital is easier to work with.) Sometimes you need to read the feedback of different sensors, or receive an image from a camera, or the input of a microphone. All of this is accomplished through Input pins.

Let me give an example to show how this would work. Lets say you have an ultrasonic sensor. An ultrasonic sensor works almost like ecolocation. It emits a wave which can bounce off of objects and return to the sensor. The sensor then calculates the time between emitting and receiving the wave and using the speed of sound as a constant can calculate the distance. (If you remember from physics, speed x time = distance). Now an ultrasonic sensor usually has 4 pins:

Vcc means voltage, and in this case you would want a stable 5v flow of electricity - which a microcontroller can give you.

GND means ground, this completes the circuit.

Trig is the pin that emits a sound wave, and echo is the pin that receives it.

Now on an arduino, you can connect the Vcc to the 5v pin, the GND to the GND, and then pick two digital or analog pins for the trig and echo. (But remember whether they are digital or analog: you will have to designate them as such in your code.) In this case, for the code we are going to use, wire the trig to 9 and the echo to 10.

Also, if you ever wire these in the future, its not recommended that you mix and match analog and digital on the same sensor, so if you put one pin in analog make sure the other is also analog. And vice versa.

Now you are going to want some sort of indicator to show you your device is working. Wire an LED to pin 11.

Ok, so now you’ve connected your sensor, lets write a simple code to read the distance using the sensor. Copy this code into the Arduino IDE.

Check my ‘//’ comments to see what the code means. As you can see, when a pin is in the ‘HIGH’ state, voltage is given to the pin. In the ‘LOW’ state, no voltage is given. Use the digitalWrite() function on arduino to control this.

Upload the code to your Arduino device which should in turn be connected to your sensor and LED.

Now we are using Input and Output in a practical way - and our sensor is working! We can check the distance in the serial monitor (top right in arduino IDE).

Now, you can do a lot more than just check distance with a sensor once you understand how input and output pins work, and how to designate them. You can use an array of sensors, 3d printed parts, motors, and servos to design complex machines to fix many problems, fast drones to entertain - or even just to learn for yourself.

Microcontrollers are a fantastic platform for electronics. I hope you learned the basics of how to use one in this post. As always - if you have any questions or comments - please don't hesitate to ask. Now that we have the basic concepts down we can move on to more complex projects. Over the next coming weeks Im going to be explaining how to build an obstacle avoiding car, so stay tuned!
Cheers