Monday, April 25, 2016

Arduino 16x2 LCD Basics

Good morning everyone, this one is a continuation of posts on Arduino and this particular one is about how to use a 16x2 Liquid Crystal Display. Let's take a look at the parts that we need for this simple project.

Parts needed:

1. Arduino (I'm using an Arduino UNO but any other one should work just fine)
2. USB Type A to Type B cable to connect the Arduino to a computer.
3. 16x2 LCD display (The one that I'm using is a White text on black background).
4. Bunch of Male to Male jumper wires.
5. Practice Board or you might know it as breadboard.
6. Male to Male header strip to attach pins to the Arduino. (Shown in the picture)
7. Couple Resistors.
8. 10Kohm variable resistor.
9. Arduino IDE.
10. Soldering Iron and Wire.

Procedure:

1. Start by attaching the Male to male header strip to the 16x2 LCD display. There should be 16 pins in there and carefully solder the strip to the display.
2. Now connect the display and Arduino via jumper wires.Wires that are needed to be connected to the display are listed below.

Pin 1 - Ground.
Pin 2 - +5V.

Pin 3 - Connect to the Wiper Terminal of the variable resistor. Variable resistor needs both Ground and +5V on it's other two terminal. This resistor is used as contrast control.

Pin 4 - Register select, connect this to Arduino pin 12.

Pin 5 - Read or Write pin. Connect it to the Ground to enable Write mode.

Pin 6 - LCD Enable Pin, connect it to the Arduino pin 11.


Pin 11 - D4 to Arduino pin 5.

Pin 12 - D5 to Arduino pin 4.

Pin 13 - D6 to Arduino pin 3.

Pin 14 - D8 to Arduino pin 2.


Pin 15 - LED+ to 5V/3.3V with a series resistor maybe a 220ohm one. I'm using a 1K one though.
Pin 16 - LED- to Ground. 


And that's it, if we connect it with the computer and upload the following code, as it is or change according to your wish we will find something like this.

 
The Code:

 // Example code taken from arduino tutorials and modified by Redwan Hasan.

// Let's start with including the LCD Library.
#include <LiquidCrystal.h>

// Initialize the library with the numbers of the interface pins.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

  // Set up the LCD's number of columns and rows.
  lcd.begin(16, 2);
 
  /* Set the cursor position to horizontal zero and vertical zero, that will give us
     the top left of the first line. This line is unnecessary however, if nothing
     is declared it will be automatically started from here. */
  lcd.setCursor(0, 0);
 
  // Print a message to the LCD.
  lcd.print(" Line 1 ");
 
  // Let's give it a vertical shift so it will be on the bottom line
  lcd.setCursor(0, 1);

  // Print a message on the bottom line.
  lcd.print(" Line 2 ");

  // to change the text just replace Line 1 and Line 2 with desired text.
}

void loop()
{
 
}


Result:

Arduino with 16x2 LCD

How does it work?

Let's start with the hardware and first of all the display. This 16x2 LCD display is a 16 pin interface module consists of 16 columns x 2 rows with CHAR. DOTS : 5 x 8 (5 Columns x 8 Rows). It has pins for powering it up, adjusting contrast, register select, read or write, enable pin, Data pins and back light power pins. This display module is interfaced with the Arduino via a 4 bit mode. So the data pin DB4 to DB7 of the LCD are connected and remaining data pins DB0 to DB3 are not used.

About the code it is pretty self explanatory yet I have added notes to the code itself.



Hope you have enjoyed. Here are couple important and interesting links.

1. Soldering Pins to the 16x2 display.
2. Read My other posts here.

Make Raspberry Pi Network Attached Storage In Easy Way

Good evening everyone. In this age of technology we all have multiple devices in a single house and we have digital contents that we consume everyday but the problem of having multiple devices is that we have to keep all those contents in each of those devices to consume them otherwise we might have to spend a lot of time transferring files in between them. So today I'm going to show you a simple and easy way to create a network attached storage using small minimum number of components although I will talk more about what else could be used in this post too. So let's look at what we need for this project.


What is a NAS?

If you look at Wikipedia you will see that it is a file level computer data storage server connected to a computer network providing data to a heterogeneous group of clients. NAS is specialized in serving files. To make it simple we can assume it as a device that is connected to your network and stores the files that you need to access from many different devices connected to the same network. This NAS allows you to keep the files in a single place and access it from multiple devices thus omitting the need for having the same file or files on each and every device. Today's wireless and wired both networks are fast enough to feed multiple clients with different types of data files whether it is high definition video, music files, pictures or maybe electronics books etc. NAS can also be implemented anywhere if something like a file server is needed.


Component List:



So what do we need to make this happen?

1. Raspberry Pi 2 (Can be done with the first release Pi or the latest release Pi 3, use whichever you have in hand),



2. Latest copy of Raspbian, fully upgraded, no matter which install method you use.



3. Some sort of power supply, I'm using a 5.2V 1.35A adapter that came with Asus made Google Nexus 7 2013.




4. For storage media I'm using couple USB thumb drives. Use which ever you want to.



5. I'm using the built-in Ethernet port so all I need is a Cat5 network cable.



6. For networking some sort of Hub/Switch/Router is needed. In this case I'm just using the router that provides wired and wireless internet in my home.



7. I'm using putty on another computer connected to the same network to SSH into the pi and use the Terminal.



Optional:

8. If you want to use portable Hard drive you need more power so I would suggest either get a powerful adapter maybe about 3A or get a powered USB hub.



9. To connect the raspberry pi via Wi-Fi you will be needing an USB Wi-Fi dongle of some sort. Good news for Pi 3 owners however, you have built in Wi-Fi.



Connection: 

Connect the one end of the Ethernet cable to the Pi and other end to your router or switch. Or if you want to use Wi-Fi just plug in the USB Wi-Fi to the Pi and provide necessary information to connect.

Plug in your storage drive or drives.
If you want to make things simpler you might want to add one drive at a time or you can just connect all the drives and do this at once. 

Power it via plugging in the micro-USB power input port.

I'm using another computer to set this whole thing up so get to a computer that is connected to the same network.


Procedure:


1. Hopefully your Raspberry Pi has booted into Raspbian. You need to find out the IP address of your pi. You can get this from the router's client list.


So I can see my raspberry pi has an IP of 192.168.2.101. Now I can move on to putty.

There is another easy way however, just type ifconfig directly on the Terminal of Raspbian or via Putty and you will get the IP address. (Although if you already have the IP to connect via Putty why would you need the IP one more time right? But if you are directly using the Raspberry Pi, not any other system then this method will be easier to get the IP address)

 


As I'm using Pi 2, I only have the wired Ethernet, if you use Pi 3 or an USB Wi-Fi dongle you might get one more option of eth1 and make sure you use the proper IP. 

[Note1: Don't worry about the mismatch of IP address in the picture above, they were done on different hardware and time but procedure will be the same. 

Note2: If you are using DHCP in your router which you probably are then your IP address for certain device might change over time which often times makes it problematic to access certain device or it is generally easier to remember one IP address for one device, if you want to make sure that your device gets the same IP address every time even while using DHCP, you can simply reserve certain IP address for certain device, see how it can be done]

2. After launching putty we will see this window, here in the host name all I need to do is to type in the IP address of the raspberry pi and hit enter. By default the port should be 22 and SSH should be selected if not select accordingly.


Next we have to login to the pi. By default the username is pi and password is raspberry. Don't worry if you can't see anything while you type the password just put the proper password and hit enter. If it is for the first time you might need to allow the connection.




3. Next we have to make sure that our Raspbian is fully upgraded.

So I will be using three different command lines for this. Which are

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade



Have patience and do this first unless it might not properly work.


4. Okay now that we have our Raspbian fully upgraded we can start installing the software that is needed. In this case we need ntfs-3g and samba. The first one is needed to mount NTFS based drives and the second one is needed to work with SMB/CIFS networking protocol. To install them we have to type in

sudo apt-get install ntfs-3g samba




5. Okay so we have installed what we have needed. Now we have to make directory where the NTFS drive or drives are going to load in. To do this we have to type in

sudo mkdir /media/New_Nas

Here instead of New_Nas put whatever you want your share folder name  to be.



6. So we created the folder where the NTFS drive is going to be mounted now we have to get UUID of those drives. UUID is a unique identifier that will identify our NTFS drives that we will be connecting and mount them properly. To get the ID we have to type in

sudo blkid



The drive that I have connected is a simple 16GB USB drive and as you can see in the putty screen that it has the same label and now we have the UUID. Copy it somewhere.


7. Next thing that we are going to do is edit the fstab so the the Raspbian operating system mount this drive during boot. To pop up the editor we have to type in

sudo nano /etc/fstab


After entering the editor we have to add in this particular line 

UUID=D690B46C90B45529 /media/New_Nas ntfs-3g defaults,noatime 0 2

Here the UUID for you will be different and the New_Nas folder might be different, the rest will be the same.


To put things simply, the first portion is for selecting the drive followed by where to load and which type of drive to be loaded. Save and exit the editor.


8. Now we are going to quickly reboot to check whether the drives load or not.

To reboot we have to type in

sudo reboot



Don't worry about the Fatal error just restart the session after couple moments.


9. Okay so we are back.

To check whether the drive is loaded properly or not we have to cd into the drive. Type in

cd /media/New_Nas

As usual you might have to change the folder name. After entering the folder to list the files or folders we have to type in

ls


For testing purpose I only had three mp4 videos in their which I can see is properly loaded. So we can move on to the next phase.


10. Now we have to type in cd to return and then we have to start configuring the samba. To do so we have to type

sudo nano /etc/samba/smb.conf


 After entering the editor we have to add in these lines at the very end of the configuration file.

[New_Nas]
comment = This is the new 16GB NTFS drive
path = /media/New_Nas
available = yes
read only = no
browseable = yes
public = yes
writable = yes
create mask = 0755




Let me go through this lines a little.

The first New_Nas is the name what you will see as a shared folder in your network.
Path sets the path from where it is going to be loaded.
Available-Writable, you can already guess.
You can add in different users if you want to, I have just made this public so that anyone who is connected to my home network will be able to access this files.

Save it and exit the editor.


11. Finally we have to reboot again so that everything starts working.

sudo reboot



Testing:

Moment of truth! Check from your other devices whether they can access the files are not.


Okay my android phone can easily browse it via ES File browser application. Now let's check from another computer.



Nice, we can get the files from my computer too!

So we have the Network Attached Storage Ready and working!


 

Note:

1. I know NTFS is not the best thing for NAS but it can get the job done but ext4 or ZFS is much better suited for this.
2. You might wanna back up the samba configuration file before editing. To do so just type in.

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak



A very important Concern:

Raspberry Pi 2, is it even the best choice for NAS!
 
Not Really, the major negative aspects of using Raspberry Pi 2 as Network Attached Storage are

1. Raspberry Pi 2 doesn’t have any USB3.0 connectivity neither it has Serial ATA port so if you have to connect any sort of storage which you will be as it is a NAS, you have to use those USB2.0 ports which do not have great data transfer speed. 

2. Another issue is the lack of Gigabit Networking capability. Storage media this days are much faster than the traditional 100M Ethernet and can easily saturate the speed so to increase the throughput a Gigabit network would come in handy but you won't get it with Raspberry Pi. Yes to get the benefit of Gigabit network you will be needing a router that supports Gigabit Ethernet and devices that can utilize that much speed.
Is there any alternative?

Unfortunately the new Raspberry Pi 3 doesn’t include any of those either so even if you get the latest one you will still have to get along with those problems or you can use the Banana Pi which has Serial ATA port so you can connect Serial ATA drive directly and it also comes with Gigabit network interface. Although the Serial ATA port found in this board is the 2.0 version which is 3Gbps but that is a decent upgrade over any USB2.0 based solution that can only theoretically reach 480Mbps.

Banana Pi


Resources:

2. UUID.
3. ZFS.

That's pretty much it, I will write more about Raspberry Pi and things similar, stay tuned!