Ex 6
Ex 6
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).
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.
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.