CHAPTER 8 : An Embedded System
Projects Examples
Subtopic
• Formulation of the problem
• Connection diagram
• Ardiono program Code
Circuit #1:
Blinking an LED
LEDs (light emitting diodes)
are small, powerful lights
used in many different
applications.
We’re going to learn how to
blink an LED on and off.
Resistor Color Code
Circuit Schematic
Pin 13 is a digital pin and can be used as an
output or an input. Here we will use it to
output power to the LED.
Pin 13 will be connected to the
positive lead on the LED.
The negative lead on the LED
will be connected to one leg of
the resistor.
The other leg of the resistor
will be connected to ground to
complete the circuit.
Note: These components are all in series (one after
the other, like beads on a string).
Design it!
Create the Sketch
• /*
• Blink
• Turns on an LED for one second then off for one second, repeatedly.
• */
• void setup() {
• pinMode(13, OUTPUT);
• }
•
• void loop() {
• digitalWrite(13, HIGH);
• delay(1000);
• digitalWrite(13, LOW);
• delay(1000);
• }
Circuit #2: Potentiometer
How to read analog input from the physical world using a
+5V
potentiometer (“pot” for short) and control the blink rate
of an LED. We’ll also learn how to use the serial monitor
to watch how the voltage changes.
When it’s connected with 5V across its two outer pins, the
middle pin outputs a voltage between 0 and 5V,
depending on the position of the knob. In this way, it can Analog
be used as a “voltage divider”. GND Input
Circuit schematic
The left side of the schematic
is the same as the previous
circuit.
We’re adding a
potentiometer to
control the blink rate of
the LED.
We’re running 5V across the outer
pins of the pot. The middle pin of
the potentiometer will be
connected to analog input pin 0.
Note: All analog pins on the Arduino are INPUT pins.
Digital pins can be INPUT or OUTPUT pins.
Design it!
Create the sketch
• int sensorPin =0;
• int ledPin =13;
• void setup() {
• Serial.begin(9600);
• pinMode(ledPin, OUTPUT);
• }
•
• void loop() {
• int sensorValue;
• sensorValue = analogRead(sensorPin);
• digitalWrite(ledPin, HIGH);
• delay(sensorValue);
• digitalWrite(ledPin, LOW);
• delay(sensorValue);
• Serial.println(sensorValue);
• }
Circuit #3
photo resistor (light sensor)
• Photoresistors change resistance
based on how much light the
sensor receives.
• Use our photo resistor in a
“voltage divider” configuration.
Output:
• High voltage = lot of light
• Low voltage = little light So…in this case we’re using a digital signal to
• Brighten and dim an LED based control an analog output.
on the light level picked up by the Wait! I thought Arduino didn’t have
a digital-to-analog converter?!?
photo resistor.
Resistive sensors & Voltage Dividers
• Arduino measures voltage, not Resistor in disguise!
resistance. Need a voltage divider:
• Consists of two resistors.
Top resistor
• The “top” resistor is the sensor.
• The “bottom” resistor is a
Bottom resistor
normal, fixed resistor (usually 10
KΩ).
When top resistor is connected to 5V and the bottom resistor to ground, the
middle will output a voltage proportional to the values of the two resistors.
This is how we use resistive sensors to measure voltage.
Pulse Width Modulation
• Sneaky trick Arduino uses to simulate the output of an analog signal.
• Arduino is so fast it can blink a pin on and off 1,000 times per second.
• PWM pins also vary amount of time blinking pin spends on HIGH vs. LOW.
• Use function: analogWrite(pin, value)
• Choose a pin marked by a ~
• Value is the duty cycle
• 0 = always OFF
• 255 = always ON
• 127 = on HALF the time
(50% duty cycle)
Pulse Width Modulation (PWM)
10% duty cycle
50% duty cycle
90% duty cycle
Circuit schematic
The left side of the schematic is
almost the same as the previous
circuits. What’s changed?
Design it!
CREATE THE SKETCH
• const int sensorPin=0;
• const int ledPin=9;
• int lightLevel, high=0, low=1023;
•
• void setup() {
• pinMode(ledPin, OUTPUT);
• Serial.begin(9600);
• }
•
• void loop() {
• lightLevel = analogRead(sensorPin);
• autoTune();
• analogWrite(ledPin, lightLevel);
• Serial.println(lightLevel);
• }
CREATE THE SKETCH
• void autoTune()
• {
• if(lightLevel<low)
• {
• low=lightLevel;
• }
• if(lightLevel>high)
• {
• high=lightLevel;
• }
• lightLevel=map(lightLevel, low+30, high-30, 0, 255);
• lightLevel=constrain(lightLevel, 0, 255);
• }
CIRCUIT #4: TEMPERATURE SENSOR
• Temperature sensors are used to measure
ambient temperature.
• Sensor we’re using has three pins – positive,
ground, and a signal. For every centigrade
degree it reads, it outputs 10 millivolts.
• We’ll integrate the temperature sensor with
Arduino and use the serial monitor to display
the temperature.
CIRCUIT SCHEMATIC
Design it!
Create the sketch
• const int temperaturePin = 0; float getVoltage(int pin)
• void setup() {
• Serial.begin(9600); {
• }
return(analogRead(pin) * 0.004882814);
• void loop() {
• float voltage, degreesC, degreesF; }
• voltage = getVoltage(temperaturePin);
• degreesC = (voltage - 0.5) * 100.0;
• degreesF = degreesC * (9.0/5.0) + 32.0;
• Serial.print("voltage: ");
• Serial.print(voltage);
• Serial.print(" deg C: ");
• Serial.print(degreesC);
• Serial.print(" deg F: ");
• Serial.println(degreesF);
• delay(1000);
• }
Output Voltage vs. Temperature for
TMP36
http://
www.analog.com/
media/en/technical-
documentation/data-
sheets/TMP35_36_37.pdf
Congratulations!
• You just learned about:
• Microcontroller basics
• Ohm’s Law
• Analog vs. digital signals
• Interpreting circuit schematics
• Designing circuits
• Basic coding
• Voltage dividers
• Pulse Width Modulation
• LEDs
• Three types of variable resistors
• How to use sensors and data sheets