Tuesday, June 18, 2019

Intro To C Programming on Raspberry Pi

C Programming on Raspberry Pi.

After seeing the heading, you might be asking why C?

And why on Raspberry Pi?

First question probably is easy to answer and it has a lot of answers. Why C?

It’s fun!

Because it might be mandatory in your syllabus.

Maybe you want to program for Arduino.

Probably small mathematical problems need to be solved.

Maybe you want to get a hang of programming and C is an awesome place to start.

So, lots of good reason for learning C.

But why on Raspberry Pi?

Here things are not that straightforward because if you have a computer even a cheap and old one will perfectly compile C codes. To learn C that computer is more than enough. Why bother with Raspberry Pi then?

Well let’s look at some scenarios, how about your computer is out of action because of a bad drive or something? Maybe Raspberry Pi can help.

How about getting distracted a lot when you want to actually gets thing done? Maybe a Raspberry Pi can help, it is not that powerful so you can’t just jump in YouTube and watch some videos and by some, I mean a lot.

What if you don’t have a computer? Well then, a Raspberry Pi should definitely help you out.

For me these are good enough reason to play with C in Raspberry Pi. I will just keep my options open should I ever need it.

Let’s look at what do we need for that
1. Raspberry Pi 2/3, Micro SD card, Power Supply, Mouse, Keyboard, Monitor.

2. Internet Connection (optional, as I think Raspbian comes with an IDE, might be wrong though)

3. Fully updated Raspbian Operating System.

Hello World!

To write down a code and compile it we need a comfortable place i.e. an IDE. It will provide us with GUI, Code Formatting etc. Heavy weight IDE that runs on PC might not run well on Raspberry Pi due to its limitation. There is however a good light weight IDE that I have been using for a long time now and serves the purpose pretty well. It is called Geany.

If it is not already installed, we need to run this simple command to install it.

sudo apt-get install geany


After installation Geany can be launched from Application Menu > Programming > Geany.

After successful launch we will be greeted with this simple GUI.

From here on we can write code and compile them. So, let’s go with the tradition and Write down a Hello World code and Compile it.

To do so we have to go to File Menu > New (With Template) > Main.c. Which will bring up an empty C file.

We can see the main function is already there so we will just add the hello world part. Don’t worry about the text in red, those are commented and won’t impact the code.

Now once we write the code, we have to save it. Do keep in mind to keep the file extension and save it in a convenient location because I will show another way of compiling the code shortly.

After saving the Code we have to Build it. If we have any mistake in the code we will be notified here.

As there was no mistake Compilation was a success. After that we can click the “Run or view current file” button to actually see the code work.

And here it is, Hello World with all its glory.


Like I was saying there is another way of showing it that is from the compiler. To do so Open up terminal and go to the location where the file is saved.

To do so you can simply cd space the location and hit enter.

After that you can list files to verify if you are in correct folder just by typing ls.

Now to show the file we will simply type

./helloworld and hit enter.

And you can see Hello World is being printed. Two things to note here

1. In the first attempt I mistakenly typed helloworld with a capital H and didn’t work. You have to make sure to use proper case for this.

2. Another interesting thing here is see how just after the Hello World in the same line we have other things? It is because we didn’t end the Hello World with any kind of “New Line”. Had we put that; we would have gotten just Hello World in that line.

Let’s not end here. Let’s do one more code. In here we will take two numbers and find the average value of them.

Though it is not a C Programming Tutorial but still let’s go through what is happening.

We first declare 3 Variables a, b, c. All of them are integer type.

Next, we use scanf to get those two values from us. We store them in variable a and b.

Then we add those two values and divide them by 2. That is the formula for finding average. We also store that data in another variable, c.

Finally, we use printf to output that variable c to terminal.

In there we put in 25 and 15 and average is 20. Which is correct.

If you are new to C, I bet this was fun.

Final Verdict:

I will be honest here, programming on Raspberry Pi is a novelty for me because I have a powerful computer but for some people it might be a necessity. One more thing, if you happen to teach people about C, how to program with C, you can hook it up with a Projector and teach them. How cool is that! Last but not the least it is fun.

Monday, June 17, 2019

Selecting Proper Micro SD Card For Raspberry Pi

For secondary storage Raspberry Pi 2 and 3 use Micro SD cards. This card contains the operating systems, application and user data of any sort. So, a fast and reliable SD card is a must for running a project without any hassle. These days however, there are like trillion SD cards on the market from billion manufacturer and a huge portion of those cards are fake. There are a few issues with these fake cards though

Extremely slow – Many of the fake micro SD cards that could be found on stores or online states speed that is too good to be true for the price and it is too good to be true. Pop it in a system and a card that advertises 95MB/s might get only 5MB/s and won’t even sustain that speed. If such a slow card is used with a Pi, the system will run sluggish.

Wrong capacity – These fake cards often advertises capacity values that are just wrong. They often employ cleverer technique than just printing wrong value on the packaging, they change that information in the firmware as well. So, if you happen to purchase one of those plugging that card in a system will state the same wrong capacity as shown on the packaging. You will only be able to catch it if you use it. For example, an 8GB card can be packaged as 64GB card, and if you try to write any larger than 8GB, it will start to delete or corrupt previous files.

Reliability – Fake cards are notorious for failure, maybe you have unplugged Pi from power directly without shutting it down properly your card can get corrupted for life.

Now don’t get me wrong, Branded cards can be unreliable and slow too, for example brands like ADATA, Transcend, Twin MOS, San Disk etc. are very popular here but many of their cards are extremely slow and unreliable. Although they do always state the proper capacity.

So, when buying a Micro SD card, it is better to go with good brand and a good model that supports good speed and is reliable. However, there is no point buying extremely fast cards like the ones used in HD/UHD cameras or stuffs like that because Raspberry Pi won’t be able to take advantage of those higher speeds. The connector and standard itself are limiting factors. To me the sweet spot is anything between 20-40MB/s write-read speed.

Methods for Powering Up Raspberry Pi

There are two methods of powering up a Raspberry Pi. First one is the simpler one and you will probably use this one more often but second one can come in handy for various projects.

First Method – Powering via Micro USB Port.

Pretty self-explanatory method, you just grab a good 5V power supply, a good cable and you are good to go. I have already spoken about the power source a little. Do follow that for selecting the proper supply.

Second Method – Powering Via GPIO pins.

Let’s look at the GPIO pin configuration first. 

We can see the 2,4,6 pins can be used to power the Pi up, although do keep in mind if you power the Pi via micro USB you can extract 5V directly off of these pins. As the 5V rail is common throughout the whole Pi we can provide power via these pins as well. Just by connecting

Pin #2 – +5V
Pin #6 – GND will get the job done.

However, if you are using longer run of cable use a 0.1μF Polymer and a 1000μF Electrolytic capacitor in parallel with these two pins, observing proper polarity for the electrolytic capacitor.
Longer run of cables might introduce electrical noises and that added polymer capacitor should help mitigating that noise.

This method is helpful when you want to use a power supply that you made maybe from a LM2576 5.0 Buck regulator! It also comes in handy when you want a good amount of current but your standard USB port won’t provide that much or a “Smart” USB port that refuses to put out that much power. Because often times these “Smart” ports will look for certain capacitance or resistance between USB Data Pins and if those are not present these supplies won’t put out their full capacity. So, if you need say 2.5A for your project, make a LM2576 based regulator and you are good to go.


Double check polarity before connecting to the Pi, also check the voltage before connecting. Reverse polarity or higher voltage can permanently damage the Pi.

Raspberry Pi PoE Power Supply:


Selecting Proper Power Supply For Raspberry Pi

Power Supply:

One of the things that often gets overlooked is the power supply unit for the Pi. This same thing happens to PC building as well. Some people don’t want to invest on a good power supply or don’t want a healthy headroom. For me I think it is absolutely necessary to have a good power supply for any PC building or project. Back to Raspberry Pi, sure, you can probably run it with your 3 years old mobile phone charger but if you want a stable experience good power source is a must. Well you can use an old phone charger if it can provide proper power. 

For powering up Raspberry Pi we want a source that provides clean power, which means DC voltage with very tight regulation and very little ripple. Even under full load we don’t want to dip below 5V and couple mV of ripple should be the max. Power source should not be that hot either as that might adversely affect the lifespan of that source. 

Current wise if you are running just a Raspberry Pi with nothing on board, so maybe just running Pi-hole on it you can go with 1A source, no problem, but as soon as you put more and more extra device and sensor on it you should consider increasing that power. In an extreme case where you might want to power up a Hard drive and one other USB device, maybe 2.5A source should be used.

If your project requires lot of USB power however, I will advise using a powered USB hub though. They are fairly cheap and works pretty well. You don’t have to worry about the speed as the Pi itself is limited to USB 2.0 spec. 


I’m sure you have seen people charging flagship android or IOS devices with torn cable, right? That is a sad scenario. We definitely want to avoid that. Cable itself might not be as important as the power supply but it is not any less important either. If it not a nicely made cable or if it is not rated for the proper current it might drop large amount of voltage across it and Pi will get a low voltage. Cheaper cables might have bad connectors too. Strong breeze might snap it off of the Pi. So, use a good quality cable that can actually carry proper amount of current and one that makes a secure connection with the Pi.