Digital System Design Lec#4
Digital System Design Lec#4
(HTI)
Mechatronics Department
Lec#4
Analog I/O Functions
analogRead(pin)
analogWrite(pin,value)
Note :The Arduino also has the capability to
output a Digital signal that acts as an Analog
signal, this signal is called pulse width
modulation (PWM). Digital Pins # 3, # 5, # 6, #
9, # 10, and # 11 have PWM capabilities. To
output a PWM signal use the command:
analogWrite().
Example
The following example reads an analog
value from an analogy input pin, converts
the value by dividing by 4, and outputs a
PWM signal on a PWM pin (Fig. Circuit
layout for analogy signal read and write)
Timer Functions
delay(ms)
delayMicroseconds(us)
Communication Functions
Serial.begin(speed)
Opens the serial port and sets the baud rate for serial data transmission. The
typical baud rate for communicating with the computer is 9600, although other
speeds are also supported, i.e., 300, 600, 1200, 2400, 4800, 9600, 14,400, 19,200,
28,800, 38,400, 57,600, or 115,200.
Communication Functions
Serial.read()
Serial.print(val)
Serial.print(val, format)
Serial.print(78) gives "78"
Serial.print(1.23456) gives "1.23"
Serial.print('N') gives "N"
Serial.print("Hello world.") gives "Hello world.“
Serial.print(78, BIN) gives "1001110"
Serial.print(78, OCT) gives "116"
Serial.print(78, DEC) gives "78"
Serial.print(78, HEX) gives "4E"
Serial.print(1.23456, 0) gives "1"
Serial.print(1.23456, 2) gives "1.23"
Serial.print(1.23456, 4) gives "1.2346"
Communication Functions
Serial.println(Value)
Serial.printIn(val,format)
Example void loop() {
// read the analog input on pin 0:
analogValue = analogRead(0);
/*
// print it out in many formats:
Analog input reads an analog input on
Serial.println(analogValue); // print as an ASCII-
analog in 0, prints the value out.
encoded decimal
created 24 March 2006
Serial.println(analogValue, DEC); // print as an
by Tom Igoe ASCII-encoded decimal
*/ Serial.println(analogValue, HEX); // print as an
int analogValue = 0; // variable to hold ASCII-encoded hexadecimal
the analog value Serial.println(analogValue, OCT); // print as an
void setup() { ASCII-encoded octal
}
Arduino Serial Print String & Variable Example
In this example, we’ll send a string text message alongside a numeric variable on the
same line. We’ll create a counter variable and print its value and keep incrementing it
and send the data over UART every 250ms
int counter = 0;
void setup() {
Serial.begin(9600);}
void loop(){
Serial.print("Counter = ");
Serial.println(counter++);
delay(250);
}
double trouble = 3.141592653589793;
float boat = 3.1415927;
void setup() {
Serial.begin(9600);
Serial.print("Float = ");
Serial.println(boat);
Serial.print("Float F.3 = ");
Serial.println(boat, 3);
Serial.print("Float F.7 = ");
Serial.println(boat, 7);
Serial.print("double = ");
Serial.println(trouble);
Serial.print("double D.3 = ");
Serial.println(trouble, 3);
Serial.print("double D.10 = ");
Serial.println(trouble, 10);
Serial.print("double D.15 = ");
Serial.println(trouble, 15);
}
void loop(){
// Do Nothing!
}
Math Functions
min(x,y)
max(x,y)
Trigonometric functions
pow(base,exponent)
Example
In this example, we create a sine wave and configure the brightness of the
LED to follow the path of the wave. This is what makes the light pulsate in
the form of a sine wave instead of just illuminating up to full brightness and
back down again (Fig.).
void loop() {
The codes are as follows
for (int x = 0; x < 180; x++) {
int ledPin = 11;
// convert degrees to radians
float sinVal;
// then obtain sin value
int ledVal;
sinVal =(sin(x *(3.1412 / 180)));
void setup() {
ledVal = int(sinVal * 255);
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, ledVal);
}
delay(25);
}
}