Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Elec 562

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ELEC 562 Embedded system design

Lab experiment 4 : Analog to digital conversion (ADC) on Arduino Uno-R3 (CO:5)

A microcontroller (likely an AVR-based one, like an Arduino) and want to read analog data
from a photoresistor and send it over serial to a PC using your own low-level serial
communication functions. Your code already includes the ADC initialization and UART
(serial) functions.

Modify it to read analog voltage from a voltage divider and send it to the PC using the
UART.

#include <avr/io.h>
#include <util/delay.h>

#define RDA 0x80


#define TBE 0x20

// Define the UART registers and bits


volatile unsigned char *myUCSR0A = &UCSR0A;
volatile unsigned char *myUCSR0B = &UCSR0B;
volatile unsigned char *myUCSR0C = &UCSR0C;
volatile unsigned int *myUBRR0 = &UBRR0;
volatile unsigned char *myUDR0 = &UDR0;

// Define the ADC registers


volatile unsigned char *my_ADMUX = &ADMUX;
volatile unsigned char *my_ADCSRB = &ADCSRB;
volatile unsigned char *my_ADCSRA = &ADCSRA;
volatile unsigned int *my_ADC_DATA = &ADC;

void setup() {
// Setup the UART
U0init(9600);

// Setup the ADC


adc_init();
}

void loop() {
// Read analog voltage from the photoresistor
unsigned int voltage = adc_read(0); // Assuming photoresistor is connected to channel 0

// Send the voltage value to the PC via UART


U0putchar((voltage >> 8) & 0xFF); // Send the high byte
U0putchar(voltage & 0xFF); // Send the low byte

_delay_ms(1000); // Delay for 1 second (adjust as needed)


}

// ... Rest of your code .

#include <avr/io.h>
#include <util/delay.h>

#define RDA 0x80


#define TBE 0x20

// Define the UART registers and bits


volatile unsigned char *myUCSR0A = &UCSR0A;
volatile unsigned char *myUCSR0B = &UCSR0B;
volatile unsigned char *myUCSR0C = &UCSR0C;
volatile unsigned int *myUBRR0 = &UBRR0;
volatile unsigned char *myUDR0 = &UDR0;

// Define the ADC registers


volatile unsigned char *my_ADMUX = &ADMUX;
volatile unsigned char *my_ADCSRB = &ADCSRB;
volatile unsigned char *my_ADCSRA = &ADCSRA;
volatile unsigned int *my_ADC_DATA = &ADC;

void setup() {
// Setup the UART
U0init(9600);

// Setup the ADC


adc_init();
}

void loop() {
// Read analog voltage from the photoresistor
unsigned int voltage = adc_read(0); // Assuming photoresistor is connected to channel 0

// Send the voltage value to the PC via UART


U0putchar((voltage >> 8) & 0xFF); // Send the high byte
U0putchar(voltage & 0xFF); // Send the low byte

_delay_ms(1000); // Delay for 1 second (adjust as needed)


}

void adc_init() {
// Set up the ADC control registers for your configuration
// Example:
*my_ADCSRA |= (1 << ADEN); // Enable ADC
*my_ADMUX = (1 << REFS0); // Set reference voltage to AVCC
*my_ADCSRA |= (1 << ADPS2) | (1 << ADPS1); // Set ADC prescaler to 64 for slower readings
}

unsigned int adc_read(unsigned char adc_channel_num) {


// Set the channel number
*my_ADMUX = (*my_ADMUX & 0xF0) | (adc_channel_num & 0x0F);

// Start the conversion


*my_ADCSRA |= (1 << ADSC);

// Wait for the conversion to complete


while (*my_ADCSRA & (1 << ADSC));

// Return the ADC result


return *my_ADC_DATA;
}

void U0init(int U0baud) {


// Set up UART for communication
unsigned long FCPU = 16000000;
unsigned int tbaud = (FCPU / 16 / U0baud - 1);

*myUCSR0A = 0x20;
*myUCSR0B = 0x18;
*myUCSR0C = 0x06;
*myUBRR0 = tbaud;
}

unsigned char U0kbhit() {


return *myUCSR0A & RDA;
}

unsigned char U0getchar() {


while (!U0kbhit());
return *myUDR0;
}

void U0putchar(unsigned char U0pdata) {


while (!(*myUCSR0A & TBE));
*myUDR0 = U0pdata;
}

int main() {
setup();

while (1) {
loop();
}

return 0;
}

This code includes the necessary functions to initialize the UART, ADC, read analog values,
and send them over serial (UART) to a PC. You can adjust the UART baud rate, ADC
configuration, and delay as needed. Please make sure your AVR microcontroller and
development environment support these registers and peripherals.

(B)

Arduino Uno

Atmega328p microcontroller is used in Arduino mega. So Arduino can be considered as a


microcontroller based development board. There are 14 digital pins which allows the digital
input/ouput devices to be interfaced with Arduino and 6 analog channels for analog devices
such as potentiometer, sensors etc.

PIR Motion sensor

This is passiv infrared sensor used to detect the human or animal motion. When the motion is
detected, the digital output will go High.

Pin Description
Vcc Power supply
Dout Digital output pin
GND Connected to ground

Arduino Programming

The basic arduino program consists of two functions

void setup()
This function is used for the configuration of the modules used for the application. Digital
pins , Serial communication modules are configured inside this function. This function will
only be executed once.

Syntax-

void setup()

Configuration statements

void loop()

This function is used for executing the operations to be performed using the controller. As the
name suggests, this function will execute till the power down or reset.

Syntax-

void loop()

Operations to be performed

pinMode() - This function is used to configure a digital pin as input or output according to
the requirement.

Syntax -

pinMode(5,OUTPUT) - This statement configures digital pin 5 as output

pinMode(6,INPUT) - This statement configures digital pin 6 as input

digitalWrite() - This function is used to write either logic high (5 V) or logic low ( 0 V) to
the digital pin which is configured as output

Syntax-

digitalWrite(5,HIGH ) - This statement write logic high value on pin 5

digitalWrite(5,LOW) - This statement write logic low value on pin 5

digitalRead()

This function read the state of a digital input pin. This returns either High or Low
Syntax -

digitalRead(6) - This function read the state of digital pin 6

i)

So the code will be,

void setup()
{
pinMode(13, OUTPUT); // Configure LED pin as output pin
pinMode(2,INPUT); // Configure the sensor pin as input pin

void loop()
{
while(digitalRead(2)==1) // When the motion detected the LED will be ON for 3
seconds and then will be OFF
{
digitalWrite(13, HIGH);
delay(3000);
digitalWrite(13, LOW);
}

Screen shot , when motion detects LED is ON

ii)

Here second LED is connected in pin 10.

Circuit diagram,

Code will be,

void setup()
{
pinMode(13, OUTPUT); // Configure LED pins as output pins
pinMode(10, OUTPUT);
pinMode(2,INPUT); // Configure pin connected to sensor as input pin

void loop()
{
while(digitalRead(2)==1) // If motion is detected then LED1 will be Off and LED2 on
for 2 seconds
{
digitalWrite(13, HIGH);
digitalWrite(10, LOW);
delay(2000);
digitalWrite(13, LOW);
}
while(digitalRead(2)==0) // If motion is not detected then LED1 will be ON and
LED2 will be off
{
digitalWrite(10, HIGH);
digitalWrite(13, LOW);
}

Screen shot (No movement detected )

When movement detected

You might also like