Elec 562
Elec 562
Elec 562
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>
void setup() {
// Setup the UART
U0init(9600);
void loop() {
// Read analog voltage from the photoresistor
unsigned int voltage = adc_read(0); // Assuming photoresistor is connected to channel 0
#include <avr/io.h>
#include <util/delay.h>
void setup() {
// Setup the UART
U0init(9600);
void loop() {
// Read analog voltage from the photoresistor
unsigned int voltage = adc_read(0); // Assuming photoresistor is connected to channel 0
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
}
*myUCSR0A = 0x20;
*myUCSR0B = 0x18;
*myUCSR0C = 0x06;
*myUBRR0 = tbaud;
}
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
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
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 -
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-
digitalRead()
This function read the state of a digital input pin. This returns either High or Low
Syntax -
i)
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);
}
ii)
Circuit diagram,
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);
}