How to interface raspberry pi with Ultrasonic Sensor

How to interface raspberry pi with Ultrasonic Sensor

This is a video tutorial about how to interface raspberry Pi with ultrasonic sensor. Ultrasonic sensor is used for distance measurement and gives fairly accurate readings as its using ultrasonic sound to find the distance. This is a detailed tutorial including all the explanation about interfacing of sensor, its working, and the algorithm of python program.

The python program is also explained in detail in the video.

To interface raspberry pi with ultrasonic sensor and create a distance meter, you’ll need

  • Raspberry Pi any version
  • Ultrasonic sensor HC-SR04
  • 1k Resistor
  • 1.5k Resistor
  • Breadboard
  • Connecting Wires

Circuit Diagram of Ultrasonic sensor with Raspberry Pi

The interfacing diagram is as shown below

Raspberry pi ultrasonic sensor interfacing circuit

Raspberry pi ultrasonic sensor interfacing circuit

Python Code for Ultrasonic Sensor Interfacing with Raspberry Pi

import RPi.GPIO as GPIO
import time
TRIG=21
ECHO=20
GPIO.setmode(GPIO.BCM)
while True:
    print"distance measurement in progress"
    GPIO.setup(TRIG,GPIO.OUT)
    GPIO.setup(ECHO,GPIO.IN)
    GPIO.output(TRIG,False)
    print"waiting for sensor to settle"
    time.sleep(0.2)
    GPIO.output(TRIG,True)
    time.sleep(0.00001)
    GPIO.output(TRIG,False)
    while GPIO.input(ECHO)==0:
        pulse_start=time.time()
    while GPIO.input(ECHO)==1:
        pulse_end=time.time()
    pulse_duration=pulse_end-pulse_start
    distance=pulse_duration*17150
    distance=round(distance,2)
    print"distance:",distance,"cm"
    time.sleep(2)    

Above code is created in Python 3, however if you’re using python 2 the code will still work for you.

Complete Video tutorial

This is the detailed tutorial of Ultrasonic Sensor Interfacing with Raspberry Pi

Keep experimenting!!!

How to interface relay with raspberry pi to switch AC devices

How to interface relay with raspberry pi to switch AC devices

If you’ve been working on raspberry pi for some time now, you must be thinking about how to make it useful in real world applications, right?

Real world applications, where raspberry pi can turn on / off a device by receiving commands either from internet of via some sensors or via a mobile phone application talking to pi using Bluetooth. The possibilities are literally limitless if we know how to perform this.

In this tutorial, we’re going to see exactly how we can interface a real world device with raspberry pi to perform switching.

Switching Devices with Raspberry Pi

If you’ve experimented on raspberry pi, then you must know that raspberry pi has a GPIO, meaning general purpose input/output port pin.

raspberry pi gpio

As you can see in the above image, the 40-pin GPIO is useful to interface a wide variety of output devices. Raspberry Pi being a digital computer, can generate logic outputs on the GPIO pins. These logic output is called as logic 0 and logic 1. Practically, when writing a program for writing logic 0 on the pin of raspberry pi, an actual 0 volt or GND potential appears on the pin. Similarly, when writing logic 1 on the pin, a +3.3V appears on the pin of Raspberry pi.

By writing a program we can generate logic 0 (gnd) or logic 1 (+3.3v) on any output pin of raspberry pi. But this output voltage is very low to turn on any physical devices. In fact the 3.3V generated on output pin is also current limited so it cannot deliver more than 20 mA current from that pin. It means, we can interface an LED directly to raspberry pi gpio pin, and thats about it. You can’t connect any other output device directly to the pin of raspberry pi, not even a DC motor. So connecting an AC Device needs something different.

Relay as Switching Circuit

relay

As said before, raspberry pi is a digital computer and the output is confined between +3.3v and 0v output levels. Therefore, we need an electronic switching circuit in order to connect any physical device to raspberry pi to perform switching. For the entire discussion of this tutorial, lets assume that we want to connect a Lamp to raspberry pi and make it On / Off. We cannot connect bulb directly to raspberry pi and hence we need a switching circuit. To switch On / Off AC devices, we need a switching circuit like relay.

A relay is an electromagnetic switch which works on DC voltage and can turn on AC as well as DC devices. The internal construction of relay looks like below schematic

as

 

relay

relay

As can be seen in above image, a simple relay is having 5 terminals

L1 and L2 are the points of an eletro-magnetic coil which acts as a magnet inside relay. The L1 / L2 can be connected directly to a DC voltage source. Whenever the coil is given with voltage, it acts as an electromagnet.

The terminal Common is a movable terminal and NC (normally closed) and NO (Normally open) are fixed terminals.

Working of Relay

The Common terminal is residing on NC terminal using a sprint tension. This is default condition of relay. If a relay is placed on a table, then the NC and COM terminals are connected to each other. Whenever the coil voltage is applied between L1 and L2, the coil gets magnetized. Due to this magnetism, the common terminal is pulled away from NC and now resides on NO. There by acting against spring tension due to magnetism. As long as relay is energized, the situation remains the same and NO and COM are connected. Whenever the relay is turned off by removal of the coil voltage, then magnetism is finished and common terminal goes back to NC Terminal

See this short gif here

relay switching

relay switching

Now as you can see, if we can properly switch On / Off relay, we can connect any AC device and make it switch along with relay. But the question is how to turn on relay itself. Most relay’s work on 12v or 24v and raspberry pi can’t generate that voltage. There are relays who work on +5v coil voltage also, but they need about 30-50mA current which any microcontroller cannot provide. Therefore we cannot directly connect relay to raspberry pi and hence, we need a switching circuit for relay itself.

Transistorized Switching Circuit for relay

For switching any DC load using raspberry pi or similar microcontroller signal, we can use transistorized switching circuits. More details about transistors can be read here

In order to use transistor for switching relay with raspberry pi, here’s the full circuit here

complete relay switching circuit

complete relay switching circuit

As shown in above circuit. The transistor is used to perform switching of relay coil and the relay terminals are used as switching points for AC Load. Using above circuit, you can connect any AC load to raspberry pi and make the device ON or OFF.’

The Components used here are

  • Q1 = NPN transistor, any normal will do, we’ve used BC548
  • R1 = 330 ohm resistor
  • D1 = simple switching Diode like 4148
  • RL1 = 12v Relay

As seen in above diagram, the Diode D1 is used as a protection device which facilitates the easy turn off of the relay.

The output form raspberry pi pin will go directly to the resistor connected to the base of transistor. Whenever the output is made HIGH, the relay will turn ON, and whenever the output is made LOW, the relay will turn OFF, here’s a program

led1 = 21 #GPIO pin to connect to relay
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(led1, GPIO.OUT)
while True:
    GPIO.output(led1, True)    
    time.sleep(2)
    GPIO.output(led1, False)
    time.sleep(2)

Relay Module

Currently, instead of making this entire circuit all by yourself, we get a direct relay module in many online stores, it looks like this

relay module

relay module

All we have to do here is give the rated voltage from power supply to this relay module and then connect the signal pin to GPIO of raspberry pi. All the related transistor and switching circuit is already present on this board. While using such relay module, just make sure that the GND of the power supply which gives power to relay module (5v/12v/24v) and the GND of raspberry pi should be made common. Means they should be connected to each other.

A simple demonstration of relay working with a lamp is shown in below video

Let me know in the comments if it was easy to understand or did you faced any difficulty in interfacing with your relay module with Pi

 

 

10 Things to do with kids at home

10 Things to do with kids at home

In this global crisis of Corona, we’re all locked down in our homes and there is really nothing to do. Thats why I decided to write about few things to do with kids

Its very difficult for us grown-ups also to stay just at home and do nothing and it does take a lot of toll on mental health as well. Moreover those with kids know situation much better. Kids are home all the time, they can’t even go and meet their friends and play something. This is very difficult. Adults at time, can cope with their mind to be completely isolated from others. But with kids, the situation is difficult to understand. They’ve never seen such situations. And probably, they’re not getting it fully why they’re locked down at home. Apart from being grateful to our kids for showing understanding, there are few things that we can do with them to spend time. And not only to spend time, but to utilize it little productively as well. So I am sharing few nicer ways to engage children at home and yet have fun at it

List of 10 Things to do with kids at home

So here’s the list of 10 things you can do with your kids right now at home

1. Play with them

10 things to do with your kids

10 things to do with your kids

As difficult as it may seem, but this is the best thing we can do with our kids right now. Just play with them. It’ll give a mental relaxation for both the parent and kids alike. A good time off from netflix and all the mobile games. There are plenty many of indoor games that you can play with you kids to stay feeling fresh and active. Here are some indoor games that you can do with your kids. Off course there are plenty of things to list here. And am sure you already are doing some of these things, but just to expand the ideas

  1. Playing Chess and Other board games
  2.  Hide and Seek
  3. Painting
  4. DIY Craft activities
  5. Carrom
  6. Simple Card Games
  7. Treasure Hunt

And believe me, your kids don’t need some real high tech video games, they need you. And no matter what you buy for them, it’ll never fill the void of spending a real time with their father or mother.

To make the play more interesting, Yacine from this website has put a very nice list of 70+ games which you can play with your kids at home. The interesting part of this blog post is there’s a printable downloadable PDF given on the same, check out it here or this link https://www.budgetdirect.com.au/interactives/kids-games/

Have fun with kids, its the no 1 thing you can do with your kids at home.

2. Cook with Kids

10 things to do with your kids

10 things to do with your kids

I don’t know if cooking is your hobby or not. But believe me, kids of almost every age loves cooking. They feel very excited to even think about an idea of cooking something themselves. And you really need no fancy dishes to make. You can start with some simple recipe including how to make tea and simple day to day stuffs. I would rather say, start with something simpler so they get interested and then move forward Here are some recommendations of activities.

  1. Make Hot milk chocolate, make an evening of entire family gathering with the hot chocolate made by your younger master or angel
  2. A Khichadi lunch prepared by your kids will make your morning fade away
  3. Make your afternoon interesting with cakes made at home. You don’t need an oven for it, you can create one with a large PAN Filled with lots of salt to produce oven like heat and bake anything
  4. Spanish omelette, very tasty and interesting to cook. Kids just love this.
  5. Noodles
  6. Roti Pizza ( using the chapati we do at home, just spread sauce, veggies and cheese on it and place on a flat PAN by covering it from top, tastes very good)
  7. Sweet and Sour Lemonade
  8. Oreo Milk shake (If you can get hold of Oreo biscuit and Vanila ice cream and some milk, just put them together and whisk in a blender, a tasty oreo shake is ready which everyone likes)
  9. Ice Candy ( Refrigerate the lemonade or any other flavored drinks made at home in a kulfi maker to make nice and crunchy ice candies)

3. Exercise with Kids

10 things to do with your kids

10 things to do with your kids

Staying just home has one very big drawback. And that is inactivity!!

Inactivity is not good for parents as well as children. In regular times, children play alot.  They jump, they run and they do hundreds of activities daily. But in a stay home situation, there is hardly any movement that kids get to do. And so is the case of parent. Not going to work, not moving out of home, will easily make one inactive robbing you of physical health. Moreover the mental depression that comes is a different story altogether. Doing light exercises with kids will keep both you and your kids happy and healthy. The exercise keeps the mental and physical well being in balance. There are many simple exercises which you can do, I’ll list some of the most effective one’s here which will give you a big energy boost and mental peace as well.

  1. Suryanamaskar, there cannot be much said about this one fantastic form of exercise / Yog Asan. read more in detail here about the great many benefits. Any kid of age 6 and above can do Surya Namaskar with you
  2. Other beneficial Yog Asana’s, learn more and watch with instructional videos here. I’d recommend do not go for any wanna be Yog teacher and instead go to an authentic source like Isha foundation
  3. Planks
  4. Crunches
  5. Jumping Jacks (with nice music, doing this just for 3-4 minutes is a great fun and workout)
  6. Dance. Even my 3 year old moves like nothing else watching his favorite songs. Better to watch videos on tv instead of that cramped out mobile screen

4. Read books with kids

10 things to do with your kids

10 things to do with your kids

If you (and) or your kids are never into reading books, this can be a very good times. No matter how much you hate books, after all all formal education we have got is through reading. We learn new recipe through reading. We learnt all our formal education through books. So there is no point denial of importance of books from life. And this can be a great chance to give this indulgence to yourself and your kids. But yes, there are limitations that if you don’t have books then its a problem. For a book worm like me and many other, the shelf’s are loaded with at least 100 books to begin with. But for a newbee, there’s a good way out. You can read it on phone. I won’t go in details of how much you or your kid should look at screen, you already know that. But I’m going to share a wonderful app on which you can read almost every famous title free of cost. Anybooks is a free android app to read books and it has almost every famous book in its ocean of libraries. I’ll recommend starting with simple stories with hidden lessons and then move to different genre. Here are some of my recommendations.

  1. Chicken Soup for the soul
  2. Harry Potter Series
  3. Ramayan, kids are thrilled to read Indian mythology
  4. The serpents Tale : Unusual tales from Mahabharat
  5. Head over to Dogo Books, a Goodreads like platform for kids

and there are literally thousands of them like this and this 

Moreover, you can switch to audiobooks as well. Listening to audio book is like watching an interesting movie which doesn’t end in 3 hours, instead runs for 10-30 or more hours. Audible has good collections of audio books as well

5. Teach computers to your kids

10 things to do with your kids

10 things to do with your kids

Depending on the age of your kids, you can start teaching them computer. If the kid is 4 years or older, its good time to start the basic mouse movements using the paint tool in windows. If the kid is in 1st std and onward, you must have started teaching her / him about typing using microsoft word and creating simple presentations. Teaching computer to your kids will be a great fun for kids of all ages and it’ll be a good long term knowledge that they’ll benefit from. Moreover, who knows, it’ll shake some dust off your minds too 😀

6. Create fun mobile apps with your kids

10 things to do with your kids

10 things to do with your kids

App Inventor is an Android App making platform provided by Google and now maintained by MIT USA. This follows the same syntax of putting blocks together as in Scratch which is a block based coding language. Whats more interesting for both parents and kids in this is they can create real apps for their phones and also download it onto their own phone. Imagine you and your kids made an app for sending sms to your wife by simply giving a single voice command, will it impress a mother and wife? I definitely thing she will. Whats most important is its endless fun to create things which you can run on your phone with your kids. Checkout app inventor here.

What all is needed to learn App Inventor: A Computer/Laptop, Android phone and Internet Connection

Who can learn App Inventor : Anyone who can use android phone, and read english

Can I teach App Inventor to my kids: Yes, if you can use android phone, and read english 🙂 🙂

7. Teach your kids programming (and yourself too)

10 things to do with your kids

10 things to do with your kids

A computer program is nothing but a way to instruct the computer to do something. This “something” can be anything from printing your name on screen to playing some sounds and also sending emails to creating games. When someone is doing programming, he’s solving some problem, he has to use paper and pencil and think on ways to achieve something. Yes, there is fixed outline in which he has to do this, but it involves 2 of the most important things

  1. Problem Solving
  2. Creative thinking

Programmer has to dwell on to something to achieve and it mugs his mind to search for answer by doing some things / writing codes. off course we don’t want to give such painstaking programming assignment to our kids, but what if that assignment is fun to do. What if it excites them and makes them want to do it more often. There are plenty good techniques to teach coding to your kids which you can read here

But if you’re serious about it, then best to start with python programming. Its simpler and both you can your kids can learn it together. You can take up an online course like this to aid you in learning python in a systematic way instead of running through jungle of online tutorials and finding right one.

8. Create videos with them

10 things to do with your kids

10 things to do with your kids

You know it for a long time. You and your kids have been watching youtube for a long time. And we know that there are plenty of youtube channels where parents and kids are making some videos. It doesn’t matter whats the content of video. It doesn’t matter if you want to make money out of it or not. Just the idea of creating a youtube video will make your kids jump with excitement and fun. Just tell them about it and see their reaction. You don’t need anything to create youtube videos and upload them except your phone. Just record and upload it on youtube. And once published, show your kids their videos on big screen, they’ll go crazy.

9. Play video game on computer

10 things to do with your kids

10 things to do with your kids

I know many will scoff at this idea. But there’s a reason I didn’t wrote “play video games”. But I wrote “play video game on computer”. Playing on a computer with a proper game controller has enourmous benefits. First video games are fun, kids absolutely love it. Moreover gaming in limited timeframe has several benefits like

  1. Improve Hand-eye co-ordination. Gaming kids learn driving faster
  2. Foster patience in kids. As they fail again and again to complete a level, they get frustrated, its a nice dosage to teach them patience. (Cuphead, one of the most difficult yet fun game)
  3. Socialism. For remotely locked down kids, playing online with their friends can be a good way to socialize them for a while (online)
  4. Joy of Competition (any and every game)
  5. Problem Solving (minecraft / little big planet)
  6. Brings parents and kids together (FIFA games)

Just make sure the games you buy for your kids are appropriate for their age groups.

10. Start a family blog

10 things to do with your kids

10 things to do with your kids

You went to a nice vacation last Christmas and have all your photos in your google photos album and you can see them once a while. Its good, yes its off course good. But what if you create a family blog of your own? What if you create a blog post of every tour you made. Every single recipe you and your kids make? Every good/bad thing happened for you? It’s a kind of journal writing itself but a very pictorial one. Blog is just a log created on internet which can have text, images and videos everything which can be viewed by just scrolling down the page. You blog has many benefits, but most importantly, it’ll be like writing your life journey on a blog. When your kids will be grown up, they’ll be looking at your entire life, how they grew up and what all they did in their childhood through your blog. Not to mention you can make money from your blog as well. But that is not the point here. Point is to give your kid a hobby and asset like no other kids by starting a blog and letting him write there.

You can create a blog with blogger or wordpress. WordPress is highly recommended. Because with wordpress, you can create a simple free blog in minutes. And in future, you can create an entire website around your blog as well. This website is also created using wordpress. 🙂

So this was my short list of engaging kids at home.  Please add in comments what you love doing with your kids and any other addition to the list and your views on this.

Thank you

How to setup a Raspberry Pi Video Streaming Server in 5 Minutes

Raspberry Pi Streaming Video Setup

  1. If you do not already have pip installed on your Raspberry Pi, please use this command to install it:
  2. sudo apt-get install python-pip
  3. Install the picamera library by running this command:
  4. pip install picamera
  5. Install the flask Python library by running this command:
  6. sudo pip install flask
  7. Download Miguel’s Flask video streaming project by running this command:
  8. git clone https://github.com/miguelgrinberg/flask-video-streaming.git
  9.  In the project folder edit the app.py file.
  10. Comment out this line by adding a “#” to the beginning
  11. #from camera import CameraUn-comment this line
  12. from camera_pi import Camera
  13. Save the file.
  14. Run this command to find out the local IP address of your Raspberry Pi.
  15. ifconfig
  16. You will see many lines of output. You are looking for this one:
  17. inet addr:192.168.0.107 Bcast:192.168.0.255 Mask:255.255.255.0
  18. The inet addr is your local IP address. In this case, 192.168.0.107.
  19.  Start the Flask server by running this command:
  20. python app.py
  21. You will see this, which means that the server is running on port 5000 and is ready.
  22. * Running on http://0.0.0.0:5000/
  23. * Restarting with reloader
  24. Open up a web browser on your favorite device and go to this address http://192.168.0.107:5000, except replace that IP address with the one that your Raspberry Pi is running on.
  25. You should see a live view the video that your Raspberry Pi is capturing.

 

How to start learning Embedded Systems : A complete roadmap

How to start learning Embedded Systems : A complete roadmap

Are you looking for how to learn embedded systems? Are you looking for a clear roadmap to start learning embedded systems? Are you a complete beginner and need no-fuss clear guidance on studying and experimenting with embedded systems

We’re going to cover all the above questions in this tutorial. And we’re going to see a clear roadmap to start learning embedded systems. We’re going to see what it takes for anyone to get started with embedded systems and what is the flow that one can follow if he or she wishes to excel in learning and developing embedded systems.

This tutorial is specially created for complete beginners to give the simplest guide to start learning embedded systems quickly and understand all that is there to learn about it. This tutorial will help you map your learning objectives based on where you are at this stage. Even if you’re in the middle of something like you understand a little bit of Arduino programming, this tutorial will help you shape your knowledge and is intended to provide guidelines to excel. So let’s get started.

What is an embedded system?

An embedded system is a dedicated computerized system that can perform one dedicated task or similar associated tasks. Let’s take an example of a temperature controller. This type of temperature controller is very commonly used in a wide variety of manufacturing industries. The typical jobs of temperature controller are

  • Read the temperature value
  • Display the temperature value
  • Configure a set point for temperature value
  • If the temperature is below the setpoint, make the output device off
  • If the temperature is above the setpoint, make the output device on
  • Repeat the cycle

As you can see in the above steps, its obvious that we do not expect an embedded system to stop functioning. An embedded system is always running unless its power off or turned off by the user. So to create such a simple system what are the options we have?

  • Analog + Digital Design
  • Embedded Design

Analog Design

Now such a system like a temperature controller, can it be built using a simple analog circuit? Yes, of course, it can be developed. By using temperature sensors, analog to digital converter, the seven-segment display drives and a lot of conditioning circuits and digital timer circuits. We definitely can create such a system for the temperature controller, but it requires a lot of design efforts. Moreover, if we want to change anything or any element of this system, it’ll be very difficult to make changes as we’ll need to reconfigure a lot of component values itself. Even if you want to add some time delay or some timed operations, you’ll need to make a lot of changes to the system. So what is the option here?

The option is instead of creating a simple circuit design, make it using embedded system principles.

Embedded Design

An embedded design if very simple to build. All it will consist of is the microcontroller unit which will do the job of sensor reading, display driving and output device (relay) switching. How it does all of this, how it prints the value and how it reads the temperature sensor and all is controlled by software written inside the controller. An embedded controller can not function directly as compared to normal analog or digital circuits. Instead, an embedded microcontroller needs a program written in its memory which will do the job of controlling the devices. The microcontroller is a reconfigurable IC. The pins of the microcontroller can do the job of input or output or analog input and so on. Depending on the software written, the pins of the microcontroller can behave differently. In order to get excelled in microcontroller coding, we need to understand the microcontroller architecture. After studying the microcontroller architecture well, we can start writing the programs for it. But because of this big limitation, many people can not dive into embedded systems directly. And here the Arduino board comes into the picture. In order to program an Arduino board, we do not need any knowledge of the underlying hardware which is called the microcontroller Board. We’ll discuss the Arduino board in detail soon.

How an embedded system is built?

So, a system that is built using this type of microcontroller as the heart of the system is called an embedded system. An embedded system has all the related input and output devices interfaced directly with the microcontroller or connected to it via some driver circuit. Depending upon the application at hand, an embedded system may have few or all of the components shown in the below diagram.

Block Diagram Of Embedded System

Architecture Of Embedded System

What are the different components of Embedded Systems?

Just take a look at the above block diagram. It’s a very simple block diagram of an embedded system. Mind well, when I say simple, it really is. The block diagram hides almost everything that is complex and gives a bird eye view of an embedded system. The embedded systems block diagram shows mainly 3 components of the embedded system.

Inputs can be usually coming from switches or sensors. In the example of temperature controller, the inputs will be switches to accept the setpoint and the temperature sensor itself. Just like, a plant watering system will have input as a moisture level sensor.

Controller is the heart of an embedded system. A controller’s job is to process the given inputs and generate outputs. For a temperature controller, the job of the controller inside is to read the temperature value, read the switches. Now compare the temperature value with a set point, and control output devices. Controller cannot do any of these things out of the Box and every controller needs software sitting inside it to do these things. This creates a demand for software programming in embedded systems.

The most popular controllers used in embedded systems are

  • Microcontroller
  • Microprocessor
  • PLDs (FPGA / CPLD)
  • System on Chip (SoC)

Outputs

This is the sole purpose of designing any system. We need to generate outputs. Usually, outputs are generated using a variety of driver circuits that can create output action. Microcontroller (if used) can only generate small signals as their output. +3.3v/0v or +5v/0v as output voltages. These voltages are very small and cannot drive any output device directly and hence we need something called as an output driver for turning things on or off. Outputs of embedded systems are in the form of below examples with their respective drivers

  • LED On / Off (No Driver)
  • Devices turning on / off. (transistor + relay driver)
  • Motors rotating with varying speed (motor driver)
  • Buzzer On-off and sound generation (no driver)
  • Fan on / off (transistor / relay / thyristor driver)
  • Sending SMS (GSM Module)
  • Sending data over the internet to for remote monitoring (wifi interface)
  • Displaying data on a screen (LCD / LED screen interfacing)
  • And many more such things

Where Arduino fit in embedded Systems?

You’re reading this tutorial means one thing is sure, you’ve heard about Arduino 😊.

In an embedded system, generally, the most emphasis on study is given for understanding the controller. The developer of embedded systems has to be very good at understanding the microcontrollers used in the system with all of their instructions and way of writing programs for them. Every microcontroller is equipped with some or all of below-mentioned features which are used for properly programming an embedded systems

  • Input / output ports
  • Timers / counter
  • Serial Ports
  • Memory (RAM / Flash / EEPROM)
  • Analog to Digital Converter
  • Digital to Analog Converter
  • Protocols interfacing (I2C / SPI)
  • Interrupts ( To make processing faster and solve programming challenges easily)
  • And many such user interfaces are on-chip

A single-chip microcontroller has all such features on the chip. In order to use any of these features, the microcontroller developer creates some unique way of configuring the microcontroller.

  • First, there are unique instructions for every microcontroller, which can execute some jobs like reading / writing data from memory / Input-output ports / all the registers inside the controller.
  • Secondly, there are many special function registers inside the microcontroller. And each feature may have one or many Special Function registers inside the microcontroller.
  • The study of the Instruction set and all the internal register organization combinedly is called a study of Architecture of microcontrollers

So every embedded systems developer has to be very good at an understanding of the microcontroller architecture as well as the knowledge of inputs and outputs to be used. Every electronics/computer engineering student usually learns this as part of their studies and anyone else who is interested to excel in microcontroller studies has to study this on their own. One thing is sure that the proper understanding of microcontroller architecture is a must.

But

What about those hobbyists/artists who don’t know about microcontrollers and still want to make embedded systems?

For exact people like these, who don’t want to study architecture and get started developing systems faster and smoother, in 2005 Arduino boards were launched in Italy. There is a long history behind Arduino boards which can be read here. The sole purpose of Arduino is to enable non-techie people to get started with designing hobby projects using embedded systems.

But over the years, due to the open-source nature of the entire Arduino system, millions of people started using it including seasoned developers for very simple reasons

  • Lots of clean libraries available for almost every single interface out there
  • Free and open source
  • Quick prototyping is possible
  • It’s cheap

As of writing this tutorial, Arduino is undoubtedly the number 1 hardware prototyping platform. And there is no need to take any survey on that. Everyone will agree with this. This is how Arduino slowly got adjusted in the crowded market of microcontrollers.

But what is Arduino anyway?

Arduino is a microcontroller-based board that is ready for usage. You don’t need to study the microcontroller architecture in order to use Arduino. You just should be able to read the board; understand basic English and you can get started with programming Arduino. I’ve written a separate tutorial about it which can be found here

Steps to Learn About Embedded Systems

Now that we’ve gotten a fair idea of how to create an embedded system, let’s jump to understand how to learn embedded systems. This will help you get a clear picture of how to get started with embedded systems and embedded programming.

Understanding of basic electronics

The first thing then is to understand basic electronics. Probably one of the most important points. Everything we’re discussing here is electronics and hence having an understanding of electronic components, their workings, and their usages will prove to be very beneficial to learn embedded systems effectively. Electronic components are used in entire embedded systems development and hence you should know about them as much as you can. But to get started, understanding of below-mentioned things is the first step.

Resistors

Resistors are those tiny little electronics components that oppose the flow of electric current. Whenever we try to build a small circuit, if a resistor is added into that circuit, the job of a resistor is to resist (or oppose) the current flow.

The value of resistance offered by a resistor never changes. Resistors are passive components means it doesn’t matter in which direction you connect it in a circuit, it will work the same way. By using resistors, we can limit the current flowing through a circuit. As seen in the previous tutorial, resistance offered by a resistor is measured in Ohms (capital omega symbol Ω)

Capacitors

An electronic component capacitor on the blue printed circuit board

A capacitor (originally known as a condenser) is a passive electronic component having 2 leads. A capacitor is used to store energy in an electric circuit. There are different types of capacitors that are used. But all they contain at least two electrical conductors plates which are separated by a dielectric i.e., insulator material. The conductors can be thin films of metal like aluminum foil. The ‘nonconducting’ dielectric material helps increase the capacitor’s charge storage. More about capacitor can be read at a very nice tutorial by adafruit here

Diodes

A diode is a unidirectional switch used in electronics. The job of a diode is to allow the flow of electricity in only one direction and blocking its flow in the other direction. Most primarily, diodes are used in a rectifier circuit. The rectifier circuit is used to convert AC voltage to DC voltage. Rectifiers are used to create power supplies. Apart from the rectifier, a diode is used in circuit protection and in a variety of applications where this conduction in one direction property is needed. More about it is here

Transistors

Transistors are used for mainly 3 types of circuits

  • Switching
  • Amplification
  • Oscillation generation

In embedded systems, transistors are mostly used for switching purposes. In analog circuits like amplifiers and oscillators, transistors server different purposes. It’s a simple 3 terminal device that comes as either PNP or NPN transistor. Their properties differ in how they conduct electricity and how it can be controlled. Transistors theory can be read here

LEDs

led

LED’s are perhaps the most interesting electronic component that we use in the circuit. LED’s are (Light Emitting Diode) components which can emit a specific light. It can be RED, Green, Blue or Yellow. LEDs require very little voltage to turn ON (typically 3v) and they have a very long life. We see LED’s in power indicators in almost any electronic appliance. The power light we see on TV, DVD, microwave, in TV remote control are all LED’s. We have LED TV’s now a day’s which uses LED’s to give the backlighting to the LCD Panel in it. More about LEDs is here

Switching devices: relays

Switching devices and circuits are needed to turn on / off devices using microcontroller output. Usually, the output of a microcontroller board like Arduino or any other microcontroller is only a digital signal. This digital signal can have a value of either 1 or 0. Electrically, 1 means the operating voltage of microcontroller and 0 means the ground voltage. Usually, 1 stands for +5v or +3.3v and 0 stands for GND of DC supply or negative of the power supply circuit. This small voltage cannot turn on / off a physical device like a light bulb. So, this voltage is given to a transistor which turns on something called Relay. This relay then can switch the major AC appliances.

Understanding of basic C Programming

A microcontroller is programmed using instructions. This is called assembly language programming. In order to write effective programs faster, C programming is used. A separate software called cross compiler is used which can convert the C program to microcontroller understandable code. This microcontroller understandable code is then created in a special file format called a hex file. This hex file is then burned or written in the permanent memory of a microcontroller called flash memory. The microcontroller reads a program from flash memory and executes the program accordingly. This whole process is called embedded C programming. There are various Integrated Development Environments for writing embedded C programming. These IDE combine the cross compiler of microcontroller and text editor to give a smooth coding experience to the programmer. The choice of IDE depends on the microcontroller being used. For example, Arduino uses the Arduino IDE. Various other microcontroller uses different IDE tools. Some of them are free some are commercial versions. Here’s a shortlist of IDE’s used for various microcontrollers

AVR series microcontrollers by Atmel (Now Microchip)

  • Atmel Studio
  • AVR-GCC Programmer notepad
  • codevisionAVR
  • mikroC Pro for AVR

Microchip PIC

  • MPLAB
  • mikroC Pro for PIC
  • CCS C Compiler

8051 and ARM architecture

  • Keil Microvision
  • Tasking
  • IAR Embedded Workbench

Your first microcontroller: Arduino

Now you got the idea of what all to study in embedded systems. It’s best to choose with Arduino if you’re a beginner. Arduino provides easy access to microcontroller features with the help of ready-made libraries and software tools. Using Arduino will be the first step to enter into the embedded systems world. In order to study about Arduino, simply grab an Arduino board and start using it. You can download your first C program to Arduino very fast, mostly within 5 minutes. In order to use Arduino, you first need to install the Arduino IDE on your computer. Arduino IDE can be found at arduino.cc Depending upon your operating system you can download the appropriate and latest version of Arduino IDE. Once you have the board and IDE installed, you’re ready to get started with coding in Arduino.

Understanding controller IO

arduino tutorials

Once you download the first program on Arduino for blinking LEDs, the next step is to understand the nature of digital Input and output. Digital input and output of Arduino works with simple 2 functions. You can generate +5v or 0v output on any Arduino pin. You can also read the input pin on any Arduino pin, but for reading voltage on any Arduino pin, it must be either 0v or +5v. Arduino reads the input as LOW or HIGH. LOW means 0 volt and HIGH means +5v. We can read input using a

  • Push-button
  • Sound sensor
  • Moisture sensor
  • Infrared proximity sensor
  • Many different modules

Reading inputs is made really simple with the digitalRead() function of Arduino.

Understanding IO interfaces, like a buzzer, Relay, digital sensors, LCD, etc

After understanding the basic digital input and output, the next step is to understand what all you can do with these functions. Now that you can read and write digital signal values, there are lots of possibilities open for you to create a number of different embedded systems. Even this much knowledge will get started with your hobby projects which can use a number of different interfaces like

  • LEDs
  • Switches
  • Sensors
  • Relays
  • Buzzer
  • Speakers
  • LCD

You don’t need to understand everything with respect to Arduino or embedded systems to create a hobby project. For example, how to create a distance meter using the ultrasonic sensor? You need just enough knowledge of Digital IO of Arduino and you can connect an ultrasonic sensor to Arduino with LCD and measure distance and print it on the screen.

Interfaces using GPIO, stepper motor, DC Motor, etc

With simple input and output, you can control many different devices except simple relays. Relays are good to turn on/off any appliance but there are many other actuators who need controlling. These actuators include stepper motors, dc / ac motors, servo motors. The primary job of a motor is to provide a rotational force. A motor converts electricity into a rotational force for performing some action. It may include a robot movement, or a 3d printer or a robotic arm. And each type of motor requires a controlled supply given to it. DC motors need an H-bridge driver circuit. AC motors need thyristorised control circuit and servo motor needs a PWM pulse.

Most of the motor’s speed can be controlled by a time-varying digital signal given to them. This signal is called PWM. PWM is pulse width modulation. PWM signal means the digital output from an output pin which is not HIGH all the time; instead, it’s continuously made HIGH / LOW with varying duration of High pulse. This high-frequency pulse is responsible for the control action of motors. Most of the microcontrollers like atmega328 (the heart of Arduino board) do have on-chip PWM capability. A clear understanding of PWM helps us design better control circuits.

Serial Port or UART

Every microcontroller like atmega328 (the heart of Arduino Uno Board) has at least one serial port on it. Serial port, technically called UART is a module on the controller chip which allows the microcontroller to perform asynchronous serial communication. This asynchronous communication is made possible with 2 wires which are TXD and RXD. Apart from connecting these 2 wires, we also need to connect the GND wire from microcontroller to the device with which we’re communicating

TXD of microcontroller goes to RXD of the device with which we’re communicating

RXD of microcontroller goes to TXD of the device with which we’re communicating.

The speed of communication needs to be set before initializing communication. Since its asynchronous communication, there is no clock shared. Hence for synchronization, baud rates or bits per second of communication have to be pre-decided and set into the controller as well as the device with which we’re performing microcontrollers serial communication. Learning about Serial port of microcontroller opens up options to interface with many interesting peripherals like

  • Bluetooth module (helps a mobile communicate with controller)
  • RFID Reader
  • GPS Receiver
  • GSM Module (send/receive SMS and calls)
  • Wifi interface

Understanding communication protocols, Serial, i2c, SPI

While studying about the devices which can be interfaced with a microcontroller, it’s very clear that only a few devices can be connected to the IO pins of any microcontroller. Because the number of input-output pins is limited, we cannot connect as many devices as we want to a microcontroller at the same time. Of course this limitation can be removed by using a higher microcontroller like atmega2560 which has more IO pin. But this can not be always done due to cost and size constraints.

In order to tackle such issues, some simple communication protocols are developed by industries to communicate a microcontroller with a number of different devices at the same time using the same common wire connection. And the most popular such communication protocol is called I2C which was developed by Philips (now NXP). Many other manufacturers call I2C as TWI. Because Philips invented I2C, only they use the name Inter IC Communication or I2C and others use a generic name like Two Wire Interface (TWI). I2C allows a microcontroller to communicate with up to 127 different devices using just 2 Wires. These 2 wires are SCL and SDA. Not to mention, the microcontroller can only communicate with one device on this bus at a time. Hence I2C is slower and usually used while interfacing with peripherals where speed is not a major concern like sensors.

Most common peripherals that support I2C protocol

  • Real-Time Clock
  • Accelerometer sensor
  • Digital Compass Sensor
  • Gyroscope sensor
  • Analog to Digital Converters
  • EEPROM Chips

Similar to I2C, there is another protocol called SPI, which was developed by Motorola. Since communication is happening on only 2 lines, I2C is slower. So when high-speed communications are needed, SPI or serial peripheral interface is required. SPI utilizes a full-duplex synchronous communication with Serial Data In, Serial Data Out, Clock, Reset and Chip select signals. SPI allows for much faster communication and usually used when high-speed data transfer is needed like memory card storage.

Popular SPI chips

  • Memory Storage, SD Card
  • EEPROM
  • Function generators
  • ADC and DAC’s

Interfaces based on communication protocols, ethernet, wifi.

With the same I2C and (or) SPI Protocols some advanced network interfaces can also be interfaced with the microcontroller which allows the device to get connected to a LAN. A connection may be to a Lan or full internet connectivity is also possible. This internet connectivity then leads to a much larger application area we call as the Internet of Things. With a microcontroller, we can interface below-mentioned network interfaces to get connected to the internet

  • Ethernet Interface Controller (SPI Protocol)
  • Wifi Interface (SPI / Serial)

Understanding microcontroller (Atmega328)

With Arduino, we can use many features of microcontroller but still not all of them. In order to make the Arduino functional, some things are disabled by the Arduino firmware and hence you don’t have access to the entire feature set of the controller. And although as powerful it may be, most professionals and product development firms still consider Arduino only for amateurs. So, if you want to get a job in Industry, then you need to know the bits and pieces of embedded development from scratch; and it begins with an understanding microcontroller. With Arduino, we got the bigger picture of what things can be done using a microcontroller, the next obvious step is to understand how all that can be done using Raw microcontroller coding. And in order to do that, you need to first choose a controller. If you’ve used Arduino Uno before, then you have already used atmega328p which is the microcontroller on Arduino. So next best thing is to open the datasheet of this controller and start reading it.

How to read microcontroller datasheet

A device datasheet is the user manual of an IC provided by the manufacturer. Just like we get a user manual when we buy a new microwave oven. Consider if the microwave oven manufacturer doesn’t give a user manual with their product, what will happen? We won’t be able to use it properly. Hence probably, we won’t buy it. While the microwave is still a very simple device, an Integrated Circuit is a very, very complex one. Hence the manufacturer provides something called a datasheet for every device they produce. The purpose of datasheet is to enable the user to understand the IC and start using it easily. In a similar context, a microcontroller is also an IC, albeit a much more complex one. So, the microcontroller also gets a datasheet. We can download the device datasheet if we know who manufactured it from their respective websites. Otherwise, you can just web search with the name of the IC and you’ll find the datasheet.

Here’s a sneak peek of Atmega328 microcontroller datasheet’s first page

The datasheet provides with every single detail about the device we’re studying. But reading datasheets directly is not very simple for a beginner. It’s very confusing because every manufacturer organizes and shows data differently. But usually, there are some common grounds whenever you view the device datasheet of any IC. Be it microcontroller or a simple Op-Amp, there are some common things that are there in the datasheet and understanding them will make things simpler. They are as follows

  • Features

This section contains a birds-eye view of the entire controller. It lists all the features of that microcontroller in a single list view. Just by reading this page, you can understand most of the features of a microcontroller.

  • Pinout Diagrams

This section usually contains the pinout diagrams of the various packages of a microcontroller. The pinout in PDIP package as well as all the other SMT versions of packages. This section gives the user the pin number and names. If you want to create a package of this device in your favorite PCB design software, then you’ll need this section. Also, it’ll help a programmer understand which pins are GPIO and which pins are for timers and serial ports and I2C and so on

  • Pin functions

In this section, the job of each pin is described in detail. We’ll understand which are power pins, which are oscillator pins and which are gpio pins. Most microcontrollers have more than one function associated with every single pin, and all of them will be described in detail over here.

  • Internal Block Diagram

This section gives an internal block diagram of the microcontroller. In theoretical studies, its called the “Architecture of Microcontroller”. The name justifies because this block diagram shows everything that is there inside the microcontroller IC. But now every circuit is laid out in detail. Instead, it gives a complete overview of all that is inside the controller. It helps to know things better about the controller.

  • Electric Specifications

This section details the different electrical specifications of a microcontroller IC. All the electrical specifications including

  • Maximum and maximum operating voltages, IO voltages
    • Operating temperature ranges
    • Minimum and Maximum Current and power consumption ratings
    • DC Characteristics

Features Explanation in detail

This is perhaps the biggest section of them all. This section shows all the workings, the internal registers descriptions, how to configure each one to be used. And how to use every single feature of the microcontroller. This section has it all. Whatever that you’re trying to use, be it an interrupt or timer, or ADC / DAC, you can use it by following this section here.

Concluding Remarks

Now that you’ve got a fair idea of what are the embedded systems, it’ll be a good idea to get started with your own experimentation. There are plenty many tutorials available on the particular microcontroller which you want to start learning embedded systems. If you want to learn embedded systems on Arduino, we have a detailed course on it here. Thanks for reading

Download this tutorial as ebook Here