Skip to main content

Posts

Showing posts with the label C PROGRAMMING TUTORIAL

🌡️ Interfacing BMP180 Pressure & Temperature Sensor with Raspberry Pi 3

🔹 Overview The BMP180 is a sensor that measures atmospheric pressure and temperature . It is commonly used in weather stations, altitude measurement, and IoT projects . In this tutorial, we will interface the BMP180 sensor with Raspberry Pi 3 and display the readings. 🛠️ Components Required ✔️ Raspberry Pi 3 ✔️ BMP180 Sensor ✔️ Breadboard & Jumper Wires 🔌 Circuit Diagram The BMP180 communicates with the Raspberry Pi using the I2C protocol . 🔗 BMP180 to Raspberry Pi Connections    BMP180 Pin Connection VCC 3.3V (Raspberry Pi) GND GND SDA GPIO 2 (I2C SDA) SCL GPIO 3 (I2C SCL) 📜 Python Code to Read BMP180 Sensor Save the following Python script as bmp180_sensor.py and run it on Raspberry Pi. python Copy Edit import smbus import time # BMP180 Default Address BMP180_ADDR = 0x77 # Open I2C bus bus = smbus.SMBus( 1 ) def read_temperature (): bus.write_byte_data(BMP180_ADDR, 0xF4 , 0x2E ) time.sleep( 0.005 ) temp_msb = bus.read_byte_data...

POINTERS EXAMPLE IN C PROGRAMMING

Simple example to understand how pointer works Syntax: Datatype *pointer_name; Example: #include <stdio.h>   int main () {    int  var = 20;    // actual variable declaration     int  *ip;        // pointer variable declaration      ip = &var;  // store address of var in pointer variable      printf("Address of var variable: %x\n", &var   );       /* address stored in pointer variable */     printf("Address stored in ip variable: %x\n", ip );       /* access the value using the pointer */     printf("Value of *ip variable: %d\n", *ip );      return 0; } Output: Address of var variable: bffd8b3c Address stored in ip variable: bffd8b3c Value of *ip variable: 20 Description: This topic seems little bit difficult but its very simple. Key poi...

ARRAY EXAMPLE IN C PROGRAMMING LANGUAGE

This example describes how to use array in c language. Syntax:      Declaration: Datatype array_name [size];      Initialization: array_name[size]; Example: #include <stdio.h> int main () {    int n[ 10 ];   // n is an array of 10 integers     int i,j;     for ( i = 0; i < 10; i++ )    {       n[ i ] = i + 100;   //  set element at location i to i + 100     }           for (j = 0; j < 10; j++ )    {       printf("Element[%d] = %d\n", j, n[j] ); //Print each array element on console    }      return 0; } Description: In this example we first simply declare an array for 10 elements, initialize it using for loop, and print all the elements on console. Line 4: This line declares an array of 10 elements.   Line 5: Declares variable Li...

SIMPLE HELLO WORLD EXAMPLE IN C LANGUAGE

1. Simple Hello world example #include<stdio.h> //  to include library "stdio.h"  int main()                    //  every c program starts with main function {  printf("Hello World"); //  printf() function prints whatever is placed within ' "  " '  return 0; } Output : Hello World Description: 1.First line "#include<stdio.h>" indicates that we want to use stdio.h library in our code. 2.int main() - all code is written in this block. "int" return type is integer. 3.printf("Hello World"); - to write a "hello world" message on console How to run and execute:  Step 1 : Type this code in your turbo c compiler and save it with appropriate file name (ex: helloWorld.c). Step 2 :Click compile -> compile Step 3 :Click run ->run Step 4 :Press alt+x to exit from turbo C and see the output. Step 5 :To continue wi...

FOR LOOP EXAMPLE IN C LANGUAGE

Simple example on how "for loop" works. Syntax: for( initialisation ; condition ; incrementation or decrementation)  {  } Initialisation  - Initialise any value Condition - Basically its till when the loop should continue. (Ex: number < 10) incrementation or decrementation : To increment or decrement any value. Example to print 10 even numbers. #include<stdio.h> int main() { int num; for(num=0;num<10;num++) //Repeat this block  {          if(num%2 == 0)          {             printf(" %d ",num);          } } return 0; } Output: 0 2 4 6 8 Description: In this example is same as while loop.We first repeat some block of statements till the value of "num" variable reaches to 10.Then we check if the current number is even and print it. for(num=0;num<10;num++) { }- this statement  first assigns zero value to num. Step 1:The...

VARIABLES EXAMPLE IN C LANGUAGE

Simple example to explain how variables are used. Syntax: int  num; float  decimal; char  alphabet; double  number ; Here int, float, char and double are datatypes(type of data). Here  num,decimal,alphabet, number are variables.  They may have any name. Example describes getting two values from user and their addition . #include<stdio.h> int main() { int num1,num2; // Global declaration printf("Enter the first number"); scanf("%d",&num1); printf("Enter second number"); scanf("%d",&num2); add(num1,num2);// Passing value to a  function "add()" return 0; } void  add(int x,int y) { int total; //  local variable total = x + y; //  performing actual addition printf("The addition of two number is: %d",total);  //Displaying addition of two numbers on screen } Output: The addition of two number is: 5 Description: Here we have used two types of variables. 1.Loca...

PRINTF SCANF EXAMPLE IN C LANGUAGE

A simple example to get input and display output in c language  Syntax: printf(" "); Anything inside double quotes "" displays message on console. scanf(""); used to get input from user. Example: #include<stdio.h> int main() { // First we declare variables for more details view "declaring c variables" int number, char character; float decimal; //Then we ask user to enter values printf("\n Enter any integer value");// Displays the message scanf("%d",&number);// Gets input from user printf("\n Enter any decimal value"); scanf("%f",&decimal); printf("\n Enter any character"); scanf("%c%",&character); printf("\n Entered values are"); printf('\n Integer %d',integer); printf('\n Decimal value: %f ',decimal); printf('\n'Character :%c",character); } Output: Enter any number: 123 Enter any decimal: 1.23 Enter any character: a Entered ...

SWITCH CASE EXAMPLE IN C LANGUAGE

How can we simplify the code when more than two if's are required ?  We use switch statements Syntax:    switch (expression)     {      case constant1:      //codes to  be executed if expression equals to constant1;     .     break;     case constant2:     //codes to be executed if expression equals to constant2;      .     break;     .    default:     //codes to be executed if expression doesn't match to any cases;  } Example: /* Program to create a simple calculator for addition, subtraction, multiplication and division */  # include <stdio.h> # include <conio.h> int main() { char operator;  //Declaring variables float num1,num2;  //Declaring variables printf("Enter operator +, - , * or / :\n");  operator=getch(); // if operator is other than +,...

WHILE LOOP EXAMPLE IN C LANGUAGE

Simple example on how while loop works. Syntax: while(condition) {} Condition : A condition is assigned to tell while loop that till when it should continue its loop. Example to print 10 even numbers. #include<stdio.h> int main() { int num = 0; while(num <=10)  //Repeat this block  {          if(num%2 == 0)          {             printf(" %d ",num);          } num++; // This simply means num = num +1 } return 0; } Output: 0 2 4 6 8 Description: In this example we first repeat some block of statements till the value of "num" variable reaches to 10.Then we check if the current number is even and print it. while(num <=10) { }- this statement  repeats whatever is written inside its block 11 times. i.e 0 to 10 . When it goes in while loop the 11th time then  if(num%2==0) - This statement means that when number is divided by 2 then remainder ...