You are on page 1of 13

DM2305

INTRODUCTION TO C PROGRAMMING
WEEK 23 (LECTURE 7)

TUTOR: DR N MANIVANNAN

1
Week 1
Introduction to the
course Week 4
Introduction to C 1. if, if-else, if-else-if
Introduction to Pelles C
2. switch-case
3. while
Week 2
SO FAR
4. do-while
Program structure
5. for
Functions
Basic flow chart
Printf and scanf instructions
Rational operators
C libraries

Week 5
Week 3 Week 6 1. Rational operators and Logical
operators
1. Data Type (char, int, float etc) 1. Bitwise operators
2. Address of a variable
2. Variables and constants 2. Data Structures 3. pointers
3. Binary numbers and 4. Arrays
conversions to Decimal numbers

2016-17 by VNM

2
EMBEDDED SYSTEM PROGRAMMING USING C

BY NUMAN CELIK
DR MANIVANNAN
DEPARTMENT OF DESIGN

Video: Introduction of Arduino


WHAT IS AN ARDUINO?

Arduino/Genuino Uno is a microcontroller board based on the ATmega328P

Features
14 Digital I/O pins
6 Analogue inputs
6 PWM pins
USB serial
16MHz Clock speed
32KB Flash memory
2KB SRAM
1KB EEPROM
THE ARDUINO IDE

The main features you need to know about are:


Code area: This is where you will type all your
code
Info panel: This will show any errors during
compiling or uploading code to your Arduino
Verify: This allows you to compile your code to
code the Arduino understands. Any mistakes
you have made in the syntax of your code will
be show in the info panel
Upload: This does the same as verify but will
then send your code to your Arduino if the code
is verified successfully
Serial Monitor: This will open a window that
allows you to send text to and from an Arduino.
We will use this feature in to read the value of
light dependent resistor (LDR) in the tutorial
SETUP THE ARDUINO BOARD
AND SERIAL PORT
STRUCTURE OF AN ARDUINO
SKETCH

void setup()
{
// put your setup code here, to run once:

}
void loop()
{
// put your main code here, to run repeatedly:

}
FIRST SKETCH ON ARDUINO ON-
BOARD LED
int onBoardLED;

void setup()
{
//Arduinos have an on-board LED on pin 13
onBoardLED = 13;
pinMode(onBoardLED, OUTPUT);
}

void loop()
{
digitalWrite(onBoardLED, HIGH);
delay(500); //delay measured in milliseconds
digitalWrite(onBoardLED, LOW);
delay(500);
}
AN EXAMPLE OF LED
BLINKING

Try make an LED pin blink in a pattern on a


pin of your choice
Detecting light level using a Light dependent
resistor (LDR)
220 components:
LDR 1 x Arduino
5k 1 x USB data/power cable
2 x LEDs (Red and Green)
2 x 220 Ohm for LEDs
1 x 5k Ohm for LDR
1 x LDR

Task:
Construct the circuit as shown
Connect the USB form the Arduino board to
the computer
Transfer the code in next slide to Sketch and
then upload onto Arduino
Control the amount of light fall onto the LDR . It
should change the LED ON/OFF status
USB connection
to computer Change the code to set different level and
experiment it to see the change.
C-CODE (SKETCH) TO CONTROL LEDS
ACCORDING THE LIGHT (LDR)
int sensorPin = A0; // select the input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
pinMode(2, OUTPUT); //output pin for RED LED
pinMode(3, OUTPUT); // output pin for GREEN LED
Serial.begin(9600); //sets serial port for communication
}

void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor:

Serial.println(sensorValue); //prints the values coming from the sensor on the screen

if(sensorValue < 110) //setting a threshold value


{
digitalWrite(2,HIGH); //turn RED LED ON
digitalWrite(3,LOW);
}
else
{
digitalWrite(2,LOW); //turn GREEN LED ON
digitalWrite(3,HIGH);
}

delay(300); // delay by 300ms.


}
More information

https://www.arduino.cc/en/Main/ArduinoBoardUno
Arduino tutorials - Youtube

Next week (week 24) Assignment Brief All must attend

You might also like