Tuesday, June 28, 2016

lLDR INTERFACING WITH RASPBERRY PI

While you can connect your analogue input/output devices to Arduino very easily, it is not so easy with Raspberry Pi (Raspi). There are no analogue GPIO pins on Raspi; all GPIOs of Raspi are digital.
The problem can be solved by adding additional analogue-to-digital converter (ADC) processors on top of Raspi; better still would be adding a small Arduino (say, Arduino nano) on top of Raspi.

Fig. 1: Circuit diagram of an analogue sensor on Raspi
Fig. 1: Circuit diagram of an analogue sensor on Raspi

However, simple resistive-type analogue-input devices can very easily be connected to Raspi by adding a small resistor-capacitor resonating circuit built around a resistor and then measuring the charging time of the capacitor. Here is a program for measuring the light intensity and then printing it out on the monitor using an LDR and a capacitor.
Interfacing the sensor
Let us say that our analogue device is a light dependent resistor (LDR) that changes its resistance as per the light intensity. For capacitor, we will use a small one, so that the charging time is precise. Any small 1μF electrolytic capacitor (which has a + lead and a – lead) will work for our purpose. With a bigger capacitor, the charging time increases, so does our measured value number. Moreover, a very big capacitor (100μF or more) may not be fully charged at all from a poor Raspi power.

Fig. 2: Pin details of Raspi B
Fig. 2: Pin details of Raspi B
Fig. 3: Screen shot of the program output
Fig. 3: Screen shot of the program output

The circuit diagram of an analogue sensor on Raspi is shown Fig. 1. Pin details of the connector on Raspi B board are shown in Fig. 2. for clarity. The capacitor works like a water tank and the LDR acts like a thin pipeline to fill it up.
The analog.py program is written in Python script, which enables the Raspi input pin to accept the signal from a sensor like LDR and output signal intensity values on the screen. The program output screen is shown in Fig. 3. The reading variable in the code is nothing but a counter. Counter value is the measurement of LDR input indirectly proportional to light intensity.
Once the capacitor gets fully charged, the attached GPIO pin (GPIO 17) senses a ‘1’ to it and the counter stops. In the code, first we set the GPIO pin as Out and then set it as Low. Then, we set the GPIO pin as In. The LDR slowly charges it to ‘1,’ and the moment the GPIO pin becomes ‘1,’ the process reverses—the reading becomes ‘0’ and the GPIO becomes Low again. We have kept a 0.5-seconds sleep time within which one cycle gets over with a clear margin.
Once that state is achieved, we reset the GPIO pin to Low and the cycle continues. Mostly, within 400ms (milliseconds), the capacitor gets fully charged and in every 0.5 seconds we reset the GPIO pin, so that the counting cycle is well clear off the reset cycle.



analog.py
import RPi.GPIO as GPIO, time, os
GPIO.setmode(GPIO.BCM)
def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.5)
GPIO.setup(RCpin, GPIO.IN)
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading
while True:
print RCtime(17) # Read RC timing using
GPIO pin #17
Direct a torchlight on the LDR and the value will change according to light intensity. There is no absolute value of measurement as such. The number varies uniformly depending upon the intensity from 75000 (dark) to 50 (direct LED torch light) and you have to make your reading from this range.
Note. The process is not very efficient. In case the Raspi computer is busy or multi-tasking, the results may vary.


Saturday, June 4, 2016

ADC INTERFACING WITH PIC12F683

#include "Includes.h"



void InitADC(unsigned char Channel)
{
ANSEL   = 0x10;     // Clear Pin selection bits
ANSEL  = 0x34;   // Select Channel
TRISIO  = 0x40;      // GP3 input, rest all output
TRISIO |= Channel; // Make selected channel pins input
ADCON0 = 0x81; // Turn on the A/D Converter

VRCON  = 0x00;
    GPIO=0x00;     // Shut off the Voltage Reference for Comparator
}



unsigned int GetADCValue(unsigned char Channel)
{
ADCON0 &= 0xf3;      // Clear Channel selection bits

switch(Channel)
{
case AN0: ADCON0 |= 0x00; break;      // Select GP0 pin as ADC input
case AN1: ADCON0 |= 0x04; break;      // Select GP1 pin as ADC input
case AN2: ADCON0 |= 0x08; break;      // Select GP2 pin as ADC input
case AN3: ADCON0 |= 0x0c; break;      // Select GP4 pin as ADC input

default: return 0; //Return error, wrong channel selected
}
   
    __delay_ms(10);      // Time for Acqusition capacitor to charge up and show correct value

GO_nDONE  = 1; // Enable Go/Done

while(GO_nDONE);     //wait for conversion completion

return ((ADRESH<<8)+ADRESL);   // Return 10 bit ADC value
}




MAIN.C 



#include "Includes.h"



// Config word
__CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_ON & CP_OFF & CPD_OFF);

#define LED  GP5   //RED LED
#define LED1 GP4    //GREEEN LED 
#define BUZZER GP0     //BUZZER

unsigned int ADC_Value,ADC_Value1;
unsigned int ADC_Total, ADC_Total1,ADC_Total2,ADC_Average,ADC_Average1;
unsigned int flag,count,ADC_Average2;
unsigned int ADCValue1,Lowerlimit,Upperlimit;



// Main function
void main()
{
OSCCON=0x61;
InitADC(AN2);       // Initialize GP2 as ADC input pin CHANNEL 2    



while(1)
{
    LED=0;
    LED1=0;
    count=0;
    ADC_Value = GetADCValue(AN2);   //taken reading for .2mv   to read
    __delay_ms(250);
    count++;



if((ADC_Value>70) &&(ADC_Value<650) && (count==1))   
   {            
 
    __delay_ms(2000);

    ADC_Value = GetADCValue(AN2);               // Initialize GP2 as ADC input pin CHANNEL 2    
ADC_Average1=ADC_Value;                       
 
Upperlimit=ADC_Average1+30;               
Lowerlimit=ADC_Average1-30;                
 
LED=1;                                  //Referance Obtained RED LED && BUZZER ON
  GP0=1; 
__delay_ms(1000); 
GP0=0;              
LED=0;
count++;
}



if(count==2)
{
while(1)
{
    //__delay_ms(2000);
ADCValue1= GetADCValue(AN2);                // GET a first ADC value to make calculation
 
                    
ADC_Average1=ADCValue1;                //STORE In A variable 



if((ADC_Average1>Upperlimit) ||(ADC_Average1<Lowerlimit))       
{
LED=0;
LED1=0;

}

else
{

GP0=1; 
__delay_ms(500); 
GP0=0;
LED=1;
LED1=1;


}

}

}
}
}

Thursday, June 2, 2016

Most Used Linux Commands for Raspberry Pi

Learning Basic Linux Commands – Raspberry Pi 

A big part of using a Raspberry Pi is also using the terminal. The terminal is something that a lot of people try to avoid, because they feel like it is a bit hard to use.
But it doesn’t need to be that that way, because in reality we can break it down to just a few basic commands that you need to know to do pretty much everything.
After you learn these commands you’ll hopefully feel really comfortable using the terminal. When you access the terminal through an SSH communication, you can install software on your Pi remotely, create files or folders and run any scripts directly from your computer.
You also don’t need to know all these commands by heart, you can always access this post as a reference to remind how to do something.
For this tutorial, I’ll be using a Raspberry Pi board with the Raspbian Lite Operating System installed.
Here’s the table of contents:
  • Exploring the Linux File System
  • Editing Files using the Terminal
  • Managing Software on Your Raspberry Pi
  • Changing the Raspberry Pi Default Settings
  • Shutting Down and Rebooting

Exploring the Linux File System

1_
It’s time to play around with the command line.
For starters, type pwd, which means print working directory:
pi@raspberry:~ $ pwd
/home/pi
The output is /home/pi. Forward slashes are always used to indicate folders and files within other folders. In this case, the current working directory is pi, which is inside home, which is inside the root of the file system. Here, pi is the username with which you are logged in.
Note: The commands in Linux are case-sensitive, which means that PWD, PwD, pWd, and any other variations are completely different from pwd. The same holds true for all other commands and for any code written in the programming languages addressed in this course.

Navigating the file system

The most frequent commands you will use are ls (list) and cd (change directory). They are used for listing the contents of a directory and moving from one directory to another.
When you fi rst open the terminal, it will open up in your home folder (as you’ve seen with the pwd command). You can display exactly what kind of files or folders are in working directory with ls:
pi@raspberry:~ $ ls
Right now your directory is empty, so you won’t see anything when you try to list your files and folders. Want to create a new folder? Usemkdir followed by the name you want to give the folder:
pi@raspberry:~ $ mkdir NewFolder
pi@raspberry:~ $ ls
NewFolder
To navigate, we’ll be using the cd command, followed by the location you want to move to. This can be done like so:
pi@raspberry:~ $ cd NewFolder
pi@raspberry:~/NewFolder $
This moved you to the NewFolder directory that you just created.
Here’s one trick you can use so you don’t have to remember the exact name of the path – the command line or terminal will try to autocomplete the phrase if you press the Tab key while something is only partially typed. Try the cd command again (use cd .. to move up one directory):
pi@raspberry:~/NewFolder $ cd .. 
pi@raspberry:~ $ ls 
NewFolder
Now start writing your cd command again…
pi@raspberry:~ $ cd NewF
… by pressing Tab when you’ve only written ‘NewF’ It will autocomplete the file path:
pi@raspberry:~ $ cd NewFolder
Finally, there are some quick commands you can use to manipulate files. Create a new file with the touch command:
pi@raspberry:~/NewFolder $ touch NewFile.txt
pi@raspberry:~/NewFolder $ ls
NewFile.txt
Individual fi les can be copied using the command cp, followed by the fi le name and you can also use this to rename files by doing:
pi@raspberry:~/NewFolder $ cp NewFile.txt OtherFile.txt
pi@raspberry:~/NewFolder $ ls
NewFile.txt OtherFile.txt
The original file can then be deleted by using the rm command followed by the file name:
pi@raspberry:~/NewFolder $ rm NewFile.txt
pi@raspberry:~/NewFolder $ ls
OtherFile.txt
You can move files using the mv command:
pi@raspberry:~/NewFolder $ mv OtherFile.txt /home/pi
pi@raspberry:~/NewFolder $ cd ..
pi@raspberry:~/NewFolder $ ls
NewFolder OtherFile.txt
There’s a lot more you can do with the command line, but these are the very basics.
As you use Linux more and more, you’ll be confronted with tasks that need the command line, and through this process you’ll learn just how much can be accomplished when you work using the command line to manipulate files.

Editing Files using the Terminal

Nano is an easy to use text editor that is installed by default in Raspbian distribution and many other Linux distributions.

Using Nano

You can run nano by just typing in nano at the command prompt.
You can use the following commands to edit the OtherFile.txt created in the previous Unit:
pi@raspberry:~ $ cd
pi@raspberry:~ $ nano OtherFile.txt
Nano will follow the path and open that file if it exists. If it does not exist, it’ll start a new buffer with that file name in that directory.
Let’s take a look at the default nano screen:
2_
At the top row, you’ll see the name of the program version number, the name and extension of the file you’re editing, and whether the file has been modified since it was last saved.
Note: If you have a new file that isn’t saved yet, you’ll see “New Buffer.”
Next, you’ll see the contents of your file.
Lastly, the final two rows at the bottom are the shortcut lines (as shown below).
3_

Shortcuts

Program functions are referred to as “shortcuts” in nano, such as saving, quitting, searching, etc. The most common ones are listed at the bottom of the screen (as shown in the preceding Figure), but there are many more that aren’t.
Warning: nano does not use the Shift key in shortcuts. All shortcuts use lowercase letters and unmodified number keys, so Ctrl+G is NOT Ctrl+Shift+G.
Press Ctrl+G to bring up the Help menu and scroll down with your arrow keys to see a list of valid shortcuts.
4
When you’re done looking at the list, hit Ctrl+X to exit Help menu.
Now let’s say you’re working on your text file and you want to save it and exit nano. This is executed by pressing Ctrl+X.
5
Nano will ask you if you want to save the changes, you can type:
  • Y, then Enter – to save all your changes
  • N, then Enter- to cancel any changes
6
This is a very brief tutorial that shows how to edit a file and save it using the nano program.
The nano is way more powerful and has a lot of shortcuts that you can use to your advantage, but those go beyond what you need to know to complete this course. You can always refer to the official documentation or use the built-in Help menu.

Managing Software on Your Raspberry Pi

7
When you know your way around the command line, downloading and installing new software on a computer or device running the Linux OS is quite easy and straightforward.
The software comes in what are called packages — software programs that can be downloaded from the Internet and installed simply by typing a command in the prompt.
To download and install these packages, you normally use a package manager, which downloads and installs not only the software you requested, but also all other required software, known as dependencies.
The Raspbian distribution uses a package manager called apt.
To manage your software, you need the authorization of the administrator, whom you already know as the superuser. To do so, type sudo (superuser do) before a command.

Updating and Upgrading

First and foremost, you have to update the list of available package versions that your package manager is aware of. (The package manager keeps such a list in the Raspberry’s file system.) Type the following command:
pi@raspberry:~ $ sudo apt-get update
You need to be connected to the Internet for this command to work. Text scrolls by after you type the command, giving information about the newest listings.
Next, you should update the software, which you can achieve by commanding apt to upgrade. This command upgrades all the packages you’ve installed to their most recent versions:
pi@raspberry:~ $ sudo apt-get upgrade
In terms of wording, the difference between updating and upgrading is subtle, but what they do is quite different (even though they’re usually done together).
sudo apt-get update updates the list of available package versions but doesn’t install or upgrade any of them, whereas sudo apt-get upgradeupdates the packages themselves, checking the list to do so. For that reason, you should always run update before upgrade.

Installing software

To install a package for which you already know the name, you have to type the following command:
pi@raspberry:~ $ sudo apt-get install <desired application name>

Running software

To run programs directly from the prompt, simply type their names, as shown in the following command:
pi@raspberry:~ $ python
This opens the python interpreter that we are going to explore in the next Module.

Removing software

To remove software from your RPi, you resort once more to the apt package manager. Here’s an example:
pi@raspberry:~ $ sudo apt-get remove <desired application name>
This command, however, leaves behind files that are somehow related to the software, such as configuration files and logs. If you don’t intend to use those files in any way, you can remove everything by using purge:
pi@raspberry:~ $ sudo apt-get purge <desired application name>
Do not remove any package that you didn’t install yourself unless you’re absolutely certain that you know what it’s for. It may be a necessary package that comes with the Linux OS, and removing it may lead to a system crash.

Changing the Raspberry Pi Default Settings

To change the the Raspberry Pi configurations you can use a tool written by Alex Bradbury. To open the configuration tool, simply run the following from the command line:
pi@raspberry:~ $ sudo raspi-config
The sudo is required, because you will be changing files that you do not own as the piuser.
You should see a blue screen with options in a grey box in the center:
9
raspi-config aims to provide the functionality to make the most common configuration changes. keep in mind that some options require a reboot to take effect. If you changed any of those, raspi-config will ask if you wish to reboot now when you select the<Finish>button.
It has the following options available:
  1. Expand Filesystem
  2. Change User Password
  3. Boot Options
  4. Wait for Network at Boot
  5. Internationalisation Options
  6. Enable Camera
  7. Add to Rastrack
  8. Overclock
  9. Advanced Options
  10. About raspi-config
Most configurations are pretty self-explanatory and for this course you only need to change one setting (as shown in the next section).

Expanding your file system

I recommend expanding your file system.
Choosing option 1 from the raspi-config menu will expand your installation to fill the rest of the microSD card, giving you more space to use for files.
Note: you will need to reboot the Raspberry Pi to make this available. Note there is no confirmation; selecting the option begins the partition expansion immediately (as shown in the Figure below).
10
Right now you don’t need to change anything else.

Shutting Down and Rebooting

There are better ways to shut down and reboot your Raspberry Pi than simply unplugging it. Unplugging your RPi is the equivalent of shutting down your computer by pressing the power button or even removing its power source, which can lead to file corruption.
To shut down your Raspberry Pi, simply type this command on the command line:
pi@raspberry:~ $ sudo poweroff
You see the following information after you use the shutdown command:
11
To reboot, type this:
pi@raspberry:~ $ sudo reboot
This is the result:
12
You need to login again through SSH after rebooting.

Wrapping Up

I encourage you to bookmark this blog post, because you don’t have to know all these commands by heart.

Wednesday, June 1, 2016

ATMEL LED INTERFACING WITH XMEGA XPLAINED A3BU

/*
 * LED.c
 *
 * Created: 01/06/2016 2:54:52 PM
 *  Author: NANDHA */ 




#include <asf.h>
#include<led.h>
#include<delay.h>
#include<gpio.h>


int main (void)
{
// Insert system clock initialization code here (sysclk_init()).

board_init();

while(1)
{
    LED_On(LED0);
delay_ms(1000);
LED_Off(LED0);
delay_ms(1000);
LED_On(LED1);
delay_ms(500);
LED_Off(LED1);
}
// Insert application code here, after the board has been initialized.
}