EEE - Unit 2 - Notes - Peripheral Interface
EEE - Unit 2 - Notes - Peripheral Interface
Peripheral Interface
CO2: DEVELOP interfacing of different types of sensors and other hardware devices with
Atmega328 based Arduino Board
ATMEGA328P is a 28 pin chip as shown in pin diagram above. Many pins of the chip here have
more than one function. We will describe functions of each pin in below table.
1 PC6 (RESET) Pin6 of PORTC Pin by default is used as RESET pin. PC6 can only be
used as I/O pin when RSTDISBL Fuse is
programmed.
2 PD0 (RXD) Pin0 of PORTD RXD (Data Input Pin for USART)
3 PD1 (TXD) Pin1 of PORTD TXD (Data Output Pin for USART)
9 PB6 Pin6 of PORTB XTAL1 (Chip Clock Oscillator pin 1 or External clock
(XTAL1/TOSC1) input)
(T1/OC0B)
16 PB2 (SS/OC1B) Pin2 of PORTB SS (SPI Slave Select Input). This pin is low when
controller acts as slave.
17 PB3 Pin3 of PORTB MOSI (Master Output Slave Input). When controller
(MOSI/OC2A) acts as slave, the data is received by this pin. [Serial
Peripheral Interface (SPI) for programming]
18 PB4 (MISO) Pin4 of PORTB MISO (Master Input Slave Output). When controller
acts as slave, the data is sent to master by this
controller through this pin.
19 PB5 (SCK) Pin5 of PORTB SCK (SPI Bus Serial Clock). This is the clock shared
between this controller and other system for
accurate data transfer.
22 GND GROUND
Interfacing an Atmega328-based Arduino board with an LED is a common beginner project and a great way to
start learning about microcontrollers and basic digital output. Here's a step-by-step guide to help you get started:
Materials Needed:
1. Atmega328-based Arduino board (e.g., Arduino Uno)
2. LED (any color)
3. 220-330 ohm resistor
4. Breadboard and jumper wires
Steps:
1. Setup the Circuit:
Insert the Atmega328-based Arduino onto the breadboard. Make sure to align the pins correctly.
Insert the LED into the breadboard. The longer leg (anode) of the LED should be connected to a
digital pin on the Arduino, and the shorter leg (cathode) should be connected to the resistor.
Connect one end of the resistor to the cathode of the LED.
Connect the other end of the resistor to the ground (GND) rail on the breadboard.
Connect a jumper wire from the anode of the LED to a digital pin of your choice on the Arduino
(e.g., pin 13).
2. Write the Arduino Code:
Open the Arduino IDE on your computer.
Write a simple Arduino sketch to control the LED. Here's an example code to turn the LED on and
off in a loop:
#include <LiquidCrystal.h>
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.print("Hello, World!");
}
void loop() {
// Your code here
}
void loop() {
Serial.begin(9600); // Initialize serial communication at 9600 bps
Serial.println("Data to send to serial monitor");
delay(1000); // Wait for 1 second
}
Open the serial monitor in the Arduino IDE (Tools > Serial Monitor) to view the data sent by your Arduino.
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 bps
}
2 . Use the Serial.print() or Serial.println() functions to send data to the serial port. For example:
1. }
2. Open the Serial Monitor in the Arduino IDE. You should see the data sent from the Arduino displayed in
the monitor window.
Receiving Data in Arduino:
To receive data from the computer or another device:
1. Initialize serial communication as before.
2. Use the Serial.available() function to check if data is available in the input buffer. You can then use
Serial.read() to read and process the received data.
arduino
void loop() {
if (Serial.available() > 0) {
char receivedChar = Serial.read();
// Process the received character
}
}
When using the Serial Monitor in the Arduino IDE, you can type text or send commands to the Arduino by
entering data in the monitor's input field and clicking "Send" or pressing Enter.
Serial communication using the Arduino IDE is a fundamental tool for debugging and interacting with Arduino
projects. It allows you to exchange data between the Arduino and a computer or other devices, making it versatile
for a wide range of applications, including data logging, sensor reading, and remote control.
void loop() {
int sensorValue = analogRead(A0); // Read analog voltage on pin A0
float voltage = sensorValue * (5.0 / 1024.0); // Convert to voltage
Serial.println(voltage); // Print the voltage to the serial monitor
delay(1000); // Wait for 1 second
}
How to do interfacing of Atmega328 based Arduino board with temperature sensor (LM35)
Interfacing an ATmega328-based Arduino board with a temperature sensor like the LM35 is a common and
straightforward task. The LM35 is an analog temperature sensor that provides an output voltage proportional to
the temperature in Celsius. Here's a step-by-step guide on how to interface an Arduino Uno (ATmega328-based)
with an LM35 temperature sensor:
Components Needed:
1. Arduino Uno or any ATmega328-based Arduino board.
2. LM35 temperature sensor.
3. Breadboard and jumper wires.
4. A 10μF capacitor (optional for noise reduction).
Interfacing Steps:
1. Wiring:
Connect the LM35 to the Arduino as follows:
LM35 VCC (Pin 1) to 5V on the Arduino.
LM35 OUT (Pin 2) to any analog input pin on the Arduino (e.g., A0).
LM35 GND (Pin 3) to GND on the Arduino.
Optionally, you can add a 10μF capacitor between the VCC and GND pins of the LM35 to reduce noise in
the sensor readings.
Make sure your connections are secure and there are no loose wires.
2. Arduino Code:
Use the Arduino IDE to write code that reads the LM35 sensor data and converts it into a temperature
reading.
Here's a simple example code to read the LM35 sensor and display the temperature in Celsius on the
serial monitor:
// LM35 Temperature Sensor interface
void setup() {
Serial.begin(9600);
}
void loop() {
// Read the analog value from the LM35 sensor
int sensorValue = analogRead(lm35Pin);
Upload this code to your Arduino board using the Arduino IDE.
3. Monitor the Serial Output:
Open the Arduino IDE, and go to "Tools" > "Serial Monitor" to open the serial monitor.
You should see the temperature readings displayed in Celsius, which are continuously updated every
second.
4. Calibration (Optional):
The LM35 sensor provides a linear output, but you may need to calibrate it for accuracy. To do this,
compare the readings with a known reference temperature and adjust your code accordingly.
Components Needed:
1. ATmega328-based Arduino board (e.g., Arduino Uno)
2. Strain gauge (e.g., a Wheatstone bridge configuration)
3. Amplification and signal conditioning circuitry (e.g., instrumentation amplifier, Wheatstone bridge
completion)
4. Breadboard and jumper wires
5. Multimeter (for calibration and troubleshooting)
Interfacing Steps:
1. Understand Strain Gauge Operation:
A strain gauge is a sensor that measures strain (deformation) on an object. It typically forms part of a
Wheatstone bridge circuit. When strain is applied, the resistance of the strain gauge changes, causing a
voltage imbalance in the bridge.
2. Wheatstone Bridge Configuration:
Ensure you have a Wheatstone bridge configuration that includes the strain gauge. This bridge
configuration provides a differential voltage output proportional to the strain.
3. Signal Conditioning:
Amplify and condition the small differential voltage output from the Wheatstone bridge.
Use an instrumentation amplifier to amplify the signal while maintaining high common-mode
rejection.
You may also need to complete the Wheatstone bridge using precision resistors to balance the
circuit.
4. Wiring:
Connect the conditioned and amplified strain gauge output to an analog input pin on the Arduino (e.g.,
A0).
Ensure that the ground (GND) of the strain gauge circuit is connected to the Arduino's ground (GND) for a
common reference.
5. Arduino Code:
Write Arduino code to read the analog voltage from the strain gauge and convert it to a strain or force
value. This conversion often involves calibration based on known reference loads.
For example, you can use the analogRead() function to read the voltage and convert it to a strain
or force value in your code.
6. Calibration:
Calibrate your system by comparing the Arduino's readings with known loads or strains. Adjust your code
to account for any deviations or nonlinearities in the strain gauge's output.
7. Monitoring and Display (Optional):
Display the measured strain or force values on the serial monitor or on an external display, such as an
LCD.
8. Testing and Debugging:
Use a multimeter to verify the strain gauge's voltage output and the conditioned signal's accuracy. Ensure
that the Arduino readings match the expected values for different loads or strains.
9. Safety Considerations:
Be cautious about the power supply and voltage levels, especially when designing the signal conditioning
circuitry. Ensure proper grounding and electrical safety practices.