0% found this document useful (0 votes)
30 views7 pages

Ex 6

The document discusses interfacing sensors to a Raspberry Pi. It covers connecting a light dependent resistor and analog to digital converter to the Pi. It also discusses installing software and coding in C to read sensor values. Finally, it introduces ARM assembly language and provides an example program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

Ex 6

The document discusses interfacing sensors to a Raspberry Pi. It covers connecting a light dependent resistor and analog to digital converter to the Pi. It also discusses installing software and coding in C to read sensor values. Finally, it introduces ARM assembly language and provides an example program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex. No.

6 INTERFACING SENSORS TO RASPBERRY PI

OBJECTIVE:
To Interface the Sensors to the Raspberry Pi circuit.

REQUIRED COMPONENTS:
● A DVI to HDMI cable
● A MicroSD card with Raspbian image loaded on it
● A mouse and keyboard
● A microUSB power supply
● A Raspberry Pi kit

BACKGROUND THEORY:
Introduction
Also known as Light-Dependent Resistor (LDR), the photoresistor adjusts its resistance
according to the light received from the environment. It works not only with sunlight, but
also with artificial light. Now let’s see how to integrate it to the real world.
Light-Dependent Resistor (LDR).

The Raspberry Pi (RPI)


With the RPi turned on, make sure it is connected to the internet by going to the Internet
Browser and trying to access google.com (don’t try to access etown.edu since the college
authorizes this communication even though it is not actually connected to the internet). If
got the connection, open Terminal.
The Terminal
● sudo apt-get update
● sudo apt-get upgrade
ALWAYS do this before messing with Terminal. It updates the libraries and commands.
This shouldn't take much time, but expect it to be a time consuming process in the future,
since more libraries, more time to check all. For this experiment, WiringPi is used to run
a C script. It works pretty much like rpi.gpio, from the last lab, but uses C.
The Hardware Setup
It is possible to connect the project as in the picture below:
The Hardware Setup.

This IC is an A/D Analog to Digital Convertor(MCP3204) and the resistor is 10K Ohm.
Use the website https://pinout.xyz/ to find the pin names so it is possible to connect to the
right place.
The Software Setup
Like python, git should have already come with the Pi. If needed install it:
● sudo apt install git-core
GIT is a version control system to track changes on the files and computer. Here is the link
to the creators website: https://git-scm.com. After downloading GIT, it is possible to get
WiringPi using this command:
● git clone git://git.drogon.net/wiringPi
To build WiringPi
● cd wiringPi
● ./build
Circuit Basics
Now lets create the Script by entering:
● sudo touch res.c sudo nano res.c
After entering this command, nano will prompt to edit res.c. For this example, the code used
can be seen below:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <unistd.h>
#define CS_MCP3208 8 // BCM_GPIO8 #define SPI_CHANNEL 0

CODING:
#define SPI_SPEED 100000 //
int read_mcp3208_adc(unsigned char adcChannel)
{
unsigned char buff[3]; int adcValue = 0;
buff[0] = 0x06 | ((adcChannel & 0x07) >> 7); buff[1] = ((adcChannel & 0x07) << 6); buff[2]
= 0x00;
digitalWrite(CS_MCP3208, 0); // Low : CS Active wiringPiSPIDataRW(SPI_CHANNEL,
buff, 3); buff[1] = 0x0F & buff[1];
adcValue = ( buff[1] << 8) | buff[2];
digitalWrite(CS_MCP3208, 1); // High : CS Inactive return adcValue;
}
int main (void)
{
int adc1Channel = 0; int adc1Value = 0;
if(wiringPiSetup() == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror(errno)); return 1 ;
}
if(wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) == -1)
{
fprintf (stdout, "wiringPiSPISetup Failed: %s\n", strerror(errno)); return 1 ;
}
pinMode(CS_MCP3208, OUTPUT);
while(1)
{
system("clear");
printf("\n\nMCP3208 channel output.\n\n"); adc1Value =
read_mcp3208_adc(adc1Channel); printf("adc0 Value = %04u", adc1Value);
printf("\tVoltage = %.3f\n", ((3.3/4096) * adc1Value)); usleep(1000000);
}
return 0;
}

To Exit nano, press Ctrl+X, it will ask if sure, Press “Y” and Enter for the res.c name.
Compile the code by entering:
● gcc -Wall -o app res.c -lwiringPi
Run the code with this command:
● sudo ./app
Output displayed in screen.

Check the res.c file. Make the prompt, instead of displaying “MCP3208 channel output.”,
display “Lab 2 – Student name” on the prompt screen. Also, before each line of “adc0
Value = …” display the counting number. The output should be something like:
Output displayed in screen.

ARM Assembly Code


Introduction
This lab is intended to introduce the ARM Architecture/Assembly Code. ARM has
become the main processor for gadgets like Smart Phones, Tablets and the best one:
Raspberry Pi. Let’s go over some examples
This is the code for the “Hello World” file
.data
string: .asciz "\nHello World!\n"
.text
.global main
.extern printf main:
push {ip, lr} ldr r0, =string bl printf
pop {ip, pc}
To assemble, link and run files on ARM assembly code Terminal is needed.
Terminal
Lets start typing the traditional:
● sudo apt-get update sudo apt-get upgrade
To use the favorite text editor in Terminal, but here nano is used.
Create the assembly1.s file by typing
● sudo nano assembly1.s
With nano opened, type the Hello World example from above.
Close the file by Pressing Control+X and when prompted to save the changes, press Y
and save the file with the same name (assembly1.s). Now that it is possible to have the
file done, lets assemble it.
● as –g –o assembly1.o assembly1.s
Now, link it.
● gcc –o assembly1 assembly1.o
The file is now assembled and linked. It is now possible to run it by entering
● ./assembly1
The screen should look like this.
Output displayed in screen.

By doing the example know how to assemble an ARM assembly code program. Write a
program that does the following:
(1) Move the integer 13 to the Register R1
(2) Move the integer 25 to the Register R2
(3) Put the sum of Register R1 and R2 to Register R3
(4) Move the value of R3 to R0
It is needed to add the lines in the end of the file. This is called a system call and is needed
to return the right value.
MOV R7, #1
SVC 0
If did everything right, in terminal it is possible to enter
echo $?
A call that outputs stored arguments
Giving the answer to the sum:
Output displayed in screen.

CONCLUSION:
Thus the Interfacing the Sensors to the Raspberry Pi circuit is done successfully.

You might also like