Tuesday 24 February 2015

Bit vs Byte and Binary Kilo vs Decimal kilo

Bit : binary digit
Byte : combination of 8 binary digits (bits)

the indication of bit is b
the indication of byte is B

k vs K
uppercase letter K stands for binary kilo which is equivalent to 1024(2^10)
lowercase letter k stands for decimal kilo which is equivalent to 1000(10^3)

Saturday 21 February 2015

MicroController Architecture

One very common classification is on the basis of a number of instructions
1.CISC :complex Instruction Set Computers
in this controller Architecture unified memory model is used for program and data

cisc
where RAM is Data Memory and ROM is Program Memory

2.RISC : Reduced Instruction Set Computers
this Controller Architectures contains Separate Memory for Program Storage and Data Storage

risc

A CISC processor often has many RISC like features

Another Classification is on the basis of way the Internal Data stored and manipulated inside CPU

I.Stack Machine
II.Accumulator Machine
III.Register Memory machine
IV.Register Register machines

lets take a computation of simple addition
c = A+B
In four different machines the computation is does as follows
I.Stack Machine

push A
push B
add
pop c


II.Accumulator Machine

load A
add b
store c


III.Register Memory Machine

load rx,a
add rx,b
store c


IV. Register Register Machine

load Rx,A
load Ry,b
add Rz,Rx,Ry
store c,Rz


Early Processors use either state or Accumulator(8051) modes.
Most Modern Processors use Register Register(Arduino) Architecture model.

Friday 20 February 2015

Arduino as Voltmeter

First let's forget about Arduino
Voltmeter is used to measure voltage between two points. we always measure/connect the voltmeter parallel to the load.

do you ever get the doubt
why do i always connect voltmeter in Parallel to the load not in Series?

voltmeter

simple answer for that is the voltage is same in the parallel circuit. The same voltage that represent in load is same that will go through the voltmeter (But Why? I didn't understand the answer for it)


Using Arduino to Measure Voltage :
Arduino ADC's are capable of Measuring voltage with-in range of 0-5 volts. so if we wanted to measure voltage below 0-5 volts we simply connect the point where we wanted to measure voltage and two grounds and by performing analogRead() we can know the voltage at that point.

But What if we wanted to measure the voltage greater than 5 volts using Arduino?
we can don this by using Voltage Divider.
Voltage divider is nothing more than two resistors connected in series with battery. As Name implies the two resistors in series are used to divide the applied voltage.
voltage divider

the applied voltage V = Vr1 + Vr2

Vr1 = V * R1 /(R1 + R2)
Vr2 = V * R2/(R1 + R2)

which is also known as voltage divider rule.

from the above formula's if we know the voltage at the One resistor we can find the overall voltage supplied to the voltage divider circuit.
from Ohm's law. v directly proportional to R.
by using different combinations of Resistor we can reduce the voltage at the measuring point at which Arduino is used to measure the voltage at R2,by using that value we can measure the overall voltage with the help of the above formula.
Arduino Voltmeter
typical combinations of R1 and R2 is 100k and 10k respectively for 0 to 55 volts measurement

Arduino ADC has an 10 Bit Resolution or 4.9 mv for unit.

The code to find the voltage is

#define R1 100000 // Value of R1 Resistor
#define R2 10000 // Value of R2 Resistor
void setup(){
Serial.begin(9600); // start the serial communication
pinMode(A0,INPUT);
}//end of setup
void loop(){
int vr2 = analogRead(A0);
vr2 = vr2 * 0.0049;
Serial.print("The voltage is");
Serial.print(vr2*(R1+R2)/R2);
}

Thursday 19 February 2015

Passing Pointers to Function In Arduino

i write the same code in three ways..

1. More descriptive way

void setup(){
Serial.begin(9600); // start the serial communication
int a = 2; // initialise the variable
int *a_ptr = &a; // initialise the pointer and assign the variable address to it
addition(a_ptr); // pass the pointer (which holds the address to the function)
Serial.println(*a_ptr); // print the value in the address which the pointer holds
}//end of setup

void loop(){
//do nothing
}//end of loop

//Function definition
void addition(int * a_ptr){ // initializing the passing argument as pointer
*a_ptr = *a_ptr+40; // take the value which is stored in address hold in the pointer and add 40 to it
}//end of addition function


2. Do we have to put the names in function argument as b?
this code is also works same as above

void setup(){
Serial.begin(9600); // start the serial communication
int a = 2; // initialise the variable
int *a_ptr = &a; // initialise the pointer and assign the variable address to it
addition(a_ptr); // pass the pointer (which holds the address to the function)
Serial.println(*a_ptr); // print the value in the address which the pointer holds
}//end of setup

void loop(){
//do nothing
}//end of loop

//Function definition
void addition(int * number){ // initializing the passing argument as pointer
*number = *number+40; // take the value which is stored in address hold in the pointer and add 40 to it
}//end of addition function


3. and One more way to do
avoiding the pointer a_ptr declaration

void setup(){
Serial.begin(9600); // start the serial communication
int a = 2; // initialize the variable a
addition(&a); // pass the address of the variable to the function addition
Serial.println(a); // print the value which is stored in the variable a
}//end of setup

void loop(){
//do nothing
}//end of loop

//addition function declaration
void addition(int * number){
//we pass the address of a variable to the function as argument
// and it is defined as pointer (which is a normal pointer declaration)

*number = *number+40; // we take the value in address and done computation using it.

}

Using Pointers In Arduino

what is a pointer ?

a pointer is a variable that holds the location of literally the address of a variable stored in computer memory


so pointer is basically a variable with special powers... different from normal variables which can store values, pointers are special variables that can hold the address of variable

when we declare a variable.. Three things happen

1.Computer memory is set aside for the variable
2.The variable name is linked to that location in memory
3.the value of variable is placed into memory

Normally we can access the address of variable in computer memory using the "&" operator.
ok let's declare a variable and check its address using the following code.
small c code (i don't know why "&" is not working in Arduino IDE so gone for the c code )

#include
void main(){
int a = 20;
printf("the value of a is %d",a);
printf("the address where a is stored in memory is %d",&a)
}


i don't have a c compiler in my pc. so i go to the online compiler and the result is
Address of a Variable
the variable holds the value it is assigned.
and the pointer holds the address of the variable.


this little code will gives us the more explanation
#include
void main(){
int a = 20;
printf("the value of a is %d",a);
printf("\nthe address where a is stored in memory is %d",&a);
int* b = &a;
printf("\n the value of the pointer is %d",b);
printf("\n the address of pointer is %d",&b);
}


and the result is
Pointer address

In pictorial Representation
Pointer Pictorial Representation

Pointer Declaration
how can we tell the compiler that we are using a pointer in Program
syntax for Pointer declaration
pointerType *pointerName

pay full attention to that asterisk (*) operator. without that we are declaring a normal variable not a pointer.


* (asterisk operator) it is useful when declaring the pointer and dereferencing it.


pointerType : It specifies the type of pointer. type of the address whose address a pointer can store.
pointer data type allocates an area in memory large enough to hold the machine address of variable.If the address of memory location in typical micro controller is 16 bits, the pointer to a character will be 16 bit value Even though the character itself is only a 8 bit value.

Once the pointer is declared we are now dealing with Address of the variable it is pointing to, Not the value of the variable itself.


pointerName: User specified Name. it is easy to read if we end the name with _ptr

we can use pointers for two purposes
1.For Accessing the address of variable
2.For accessing the value of the variable whose memory address the pointer stores.
#include
main(){
//initialize the pointer
int a = 20;//initialize the integer whose address has to be stored in the pointer
int *a_ptr = &a;//assign the address of variable a to the pointer variable a_ptr
printf("the address stored in a_ptr is %d",a_ptr);
printf("\nthe value at which address is stored in pointer is %d",*a_ptr)
}


Re

In title i say Arduino But all the codes used in this post is using C. I hope This little Arduino Code will make sense after knowing little about Pointers.

void setup(){
Serial.begin(9600);// start the serial Communication
int a = 20; // initalize the Variable a
int *a_ptr = &a; // Initialize the a_ptr and assign the address of variable a to it
int b = *a_ptr;// assign the value at which the address is stores in pointer to the variable b
Serial.print("The Value of a is\t");
Serial.print(a);
Serial.println("");
Serial.print("The Value of b is \t");
Serial.println(b);
Serial.println("");
}
void loop(){
//do nothing here
}

Arduino code

Tuesday 10 February 2015

Serial Communication.....What is it?

what is Serial communication?
we can go through that But..
first what is communication?
communication is way of exchanging the information. information is in any form the languages, radio waves, machine code. we humans communicate using languages. radio devices communicate using radio waves. computers and controllers with in radio or any device communicate using machine code.

if i talk in English and the other person before me talking with Another Language like Telugu, we ended up fighting each other why ? it does make any sense to both of us if we talk in different languages with each other... it is not a proper way of communicating.

so what is proper way of communicating.. is following some standard of rules while exchanging the data each other.. so first we need to know the rules


what is serial?
serial in simple terms means One after another..

Normally in data communication there two ways to transfer the data..
Serial and Parallel.

all the computers and processors are internally exchange data and commands using parallel communication.
But Serial communication has the advantage of less lines when compared to the parallel communication i.e if we wanted to send 8 data bits using 8 bit parallel communication we requires 8 wires, In serial communication we only require 3 wires : one for data , second line for the ground and possibly third line for clock.

Disadvantage of serial communication is low data transfer rate when compared to parallel communication.

Serial Communication Protocol
like before we said we need to follow some standard of rules to achieve proper way of communication. the classy word for these rules are PROTOCOL

In any communication the receiver must know what kind of data to expect and at what rate?


There are two general strategies for communicating : Asynchronous and Synchronous. Each has its advantages and disadvantages.

Asynchronous communication take place outside of real time. For example, a learner sends you an e-mail message. You later read and respond to the message. There is a time lag between the time the learner sent the message and you replied, even if the lag time is short.

In contrast, synchronous, or real-time, communication takes place like a conversation.

In Serial Communication there also two ways:
Synchronous Serial Communication like SPI
and Asynchronous Serial communication like UART

Monday 9 February 2015

Two ways Arduino && 8051

I am trying the things which the many of people already did many times with Arduino.... It is very easy to do the same things others have done But Sometimes i got this thought "What the Hell iam really doing with this Little arduino Chip".
Really it doesn't making any sense by using all the Makeup commands in the Arduino. I wanted to know what iam really doing with this little chip.
So i tried to figure-out something in Data Sheet But The Datasheet for Atmel328 Controller is Freakshinly big. It is almost more than 600 pages.

So I changed my mind to go through arduino datasheet and gone for 8051 family.

I don't wanted to go forward using hands on examples first i wanted to learn something about 8051 architecture.
what it is?
how every thing is working?
just wanted to learn every thing understandable to me.

and Whatever i write here is not my own brain... Its a collection of many websites and blog's i gone through while i am digging the net about 8051.

Sunday 8 February 2015

Temperature Measurement using K-type Thermocouple and Arduino

For Industrial temperature Measurement Thermocouples are Best choice because of its robust nature, wide range of temperature measurement and no self heating...
But for Beginners like me Dealing with Thermocouples is a nightmare because of all the hurdles of noise reduction and cold junction compensation.

Please go through the wikipedia page or google to know what is noise involvement in thermocouple and cold junction compensation because I barely know what it is and I dont want to deal with them Now.

Why...?

I learn that a nice IC like Max6675 will take care of both of them (But Expensive... ) and it looks like a best route to go through the thermocouple this time.

From datasheet::
The MAX6675 performs cold-junction compensation and
digitizes the signal from a type-K thermocouple. The data is output in a 12-bit resolution, SPI™-compatible,
read-only format.


so what i wanted to do is to just connect thermocouple wires to the IC and let the IC take care of Temp Measurement and get the reading from Max6675 IC's SPI interface....

Ok.. that's a super easy thing and let's go take a look at the Pin configuration
Max 6675 Pin configuration

there are just 8 pins and 3 of them are GND(pin 1) , VCC(pin 4) and one No Connection Pin(Pin 8)

and two pins to connect our thermocouple. T+(Pin 2) and T-(Pin 3)

and Three Pin's to read the Temp data from Ic i.e SPI connection Pins. SCK (pin 5), CS(pin 6) and SO (pin 7)

as already said we get temp data via SPI.. Ok that's good.. Not enough Information to proceed further.
what we wanted to know is In What Format we are going to receive the data?
In datasheet

the max6675 process the reading from thermocouple and transmit the data through the serial interface
Force CS low and apply clock signal at sck to read the result at SO


ok.. what does it mean by force CS to low ? it means simply sending a logic 0 to the CS pin.(Note that CS is a active low input)

after doing that the result will be available at SO. But In what format?

The first bit, D15, is a dummy sign bit and is always
zero. Bits D14–D3 contain the converted temperature in the order of MSB to LSB. Bit D2 is normally low and
goes high when the thermocouple input is open. D1 is low to provide a device ID for the MAX6675 and bit D0
is three-state.


max6675 Serial configuration

so we need total 16 pulses to read the entire data on SO pin and bits D3 - D14 contain Our information.

the code to do the temp measurement is..

#define cs_pin 10
#define so_pin 12
#define sck_pin 13

void setup(){
Serial.begin(9600);
pinMode(sck_pin ,OUTPUT);
pinMode(cs_pin,OUTPUT);
digitalWrite(cs_pin,HIGH);// cs_pin is a active low pin
pinMode(so_pin,INPUT);
}


void loop(){
Serial.println("Getting the Temperature Value");
Serial.print("Temp in C is -->");
Serial.print(get_value());
Serial.println();
}

//defining the get value function
float get_value(){
digitalWrite(cs_pin,LOW);
delay(2);
digitalWrite(cs_pin,HIGH);
//give some time for conversion to take place
delay(220);//220 milli seconds delay - Refer Data Sheet
// now we have our reading ready..
// we can read the data from so line by sending clock pulses from sck pin
read_dummy_bit();
int temp = read_temp_data();
read_open_TC_bit();
read_remaining_bits();
//disable the device
digitalWrite(cs_pin,HIGH);
// calculate the temp value
float temp_value = temp *0.25; // why 0.25 ?
// the resolution of Max 6675 is 0.25 C
return temp_value;
}//end of get_value() function

//read_dummy_bit() function
void read_dummy_bit(){
digitalWrite(sck_pin,HIGH);
digitalRead(so_pin); // read the value and discard it.
digitalWrite(sck_pin,LOW);
}//end of read_dummy_bit() function

//read_temp_data() function
int read_temp_data(){
int value = 0;
for (int i = 11; i <= 0; i--){
// why are we decrementing and shifting in the descending order ..?
// because we receive the MSB first
digitalWrite(sck_pin,HIGH);
value += digitalRead(so_pin) << i;
digitalWrite(sck_pin,LOW);
}//end of for loop
return value;
}//end of read_temp_data() function

//read_opn_TC_bit() function
void read_open_TC_bit(){
digitalWrite(sck_pin,HIGH);
if(digitalRead(so_pin)){
Serial.println("Please check the connection of thermocouple wires");
}//end of else
digitalWrite(sck_pin,LOW);
}//end of read_open_TC_bit() function

//read_remaining_bits() function
void read_remaining_bits(){
for(int i=0;i<=1;i++){
digitalWrite(sck_pin,HIGH);
delay(1);//just leave the value we dont need them
digitalWrite(sck_pin,LOW);
}//end of for loop
}//end of read_remaining_bits() function

I will explain the code in the next post

Saturday 7 February 2015

74HC4051 Multiplexing/Demultiplexing Analog IC

Even Though 74hc4051 performs both multiplexing and demultiplexing... I dont want to discuss Demultiplexing Now... because i don't need it now. if i need it any time i didn't mind coming back.

multiplexing is choosing from one of various lines (one at a time) and forwards its contents down a single line i.e whatever voltage on it is forwarded to the Common Out/In line


the pinout of 74HC4051 is
Capture

74hc4051 is a useful device which can multiplex and demultiplex up to 8 analog signals into a single analog signal.

in the above pinout A0 to A7 serve as Inputs and A serve as output (pin3)
s0,s1,s2 serve as Address select inputs which are used to send the selected input to Output
E(pin 6) is a active low input which is used to Enable or Disable the IC
Vcc and Gnd are power and ground connections
VEE is the negative supply. which is used to forward the negative input voltage to output
in my case negative voltage is not applied to Arduino So I don't want to bother with
it. so i just connect with the Ground.

So if we connect the pin6,7,8 to the ground and s0,s1,s2 to the arduino output pins and pin3 (A) to the Input port pin we are good to go.

by selecting each combination of s0,s1,s2 we can send the respective input to the output of the Mux....

Multiplexing Max6675 for multiple thermocouple measurements

while i am digging the net to learn something about temperature measurement using thermocouples i got into this ic max6675.

In the starting while reading about thermocouples everywhere they say they are inexpensive but the ic i gone through costs more than 1000 Rs each.. and i don't think it is inexpensive to include the microcontroller to read and the k type thermocouple wire and display. if i included all of them the total cost is no near to the inexpensive.

so i did look for the alternatives in net, they give me so much circuit where i didn't understand one of them so i come back to the maxims max6675.

and again i wondered what if i wanted to measure the temperature at two or more different places with only including cost of k type thermocouple wire.. i think it looks interesting what i wanted to do is

take a max6675 ic that do cold junction compensation and gives the temperature of the thermocouple wires connected to them, i want to multiplex the two or more temperature measurement wires and pass to the max6675 one at each time to measure the temperature.

Capture

Wait a minute..
if I connect an another IC between thermocouple wires and Max6675IC doesn't it form a another 2 junctions which may be result garbage values at the display.....
So
it won't work


I concluded it.. But Suddenly i went through something like thermocouple law of intermediate metals



According to the Thermocouple Law of
Intermediate Metals, illustrated in Figure below,
inserting any type of wire into a thermocouple
circuit has no effect on the output as long as both
ends of that wire are the same temperature, or
isothermal.




Capture

So if i assume that both IC's i.e multiplexer IC and thermocouple cold junction compensation ic is at same temperature, we can measure various temperatures at different locations using single Max6675 IC.