Arduino Basics
Serial.print() function
The purpose of this function is to print data on the serial monitor.
So it prints data to the serial port as human readable ascii text.
Syntax
Serial.print(val)
Serial.print(val, format)
Examples
By default serial print function gives decimal values.
Serial.print(78) gives 78
Serial.print(1.23456) gives 1.23
Serial.print(‘N’) gives N
Serial.print(“Hello”) gives Hello
If we pass a value in the format,
Then we can have different values as per format.
Format is an integral data type. It can have byte, char, int, long,
short, unsigned char, unsigned int, unsigned long, word.
We can use four types of data in format.
Binary (BIN)2, Octal (OCT)8
Decimal (DEC)10, Hexadecimal (HEX)16
Examples
We can also pass the number of decimal places to be printed for
floating point numbers (double, float)
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.2345
Serial.println()
It does the same functions as Serial.print() and in addition to that
it always prints value on the new line
digitalWrite() function
We can use this function to on/off relays, leds, or dc motars.
These devices work on 5V so we can use this function to operate
all these devices.
In arduino, 0 - 13 pins are digital pins.
We can use these pins to get input/output.
Syntax
digitalWrite(pin, value)
Using this function, we can do high or low on any pin.
Value for any pin can be either 0 or 1, that is low or high.
Here, 1 means 5V and 0 means 0V
For example, I have to make pin 13 high and low.
digitalWrite(13, 1)
digitalWrite(13, 0)
digitalWrite(13, LOW)
digitalWrite(13, HIGH)
We can use both ways.
Example Code.
Pin Number 13 in arduino is connected to led by default.
So, first we’ll decide on which pin we want the output.
Here, we want output on pin number 13.
So, we can use this function.
void setup(){
pinMode(13, OUTPUT);
}
Now we want the led to be high for some time and low for some
time, and we want to run this in a loop. So, we can use void
loop() which is like a main function for arduino.
void loop(){
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Now the delay function says I’ll wait for some milliseconds after
the code line before me has been executed.
So, here our led will be on for 1s and then off for 1s in a loop.
digitalRead() function
Syntax
digitalRead(Pin)
We have digitally read a pin. We’ll use this function for those
devices that gives us a digital value.
For example, a button. It can either give 0 or 1.
And motion sensor also gives output as 0 or 1.
To read these 0 or 1 values, we use this digitalRead function.
We also have an IR sensor that also gives 0 or 1 output.
They give low or high values only.
To read this type of data only, we use the digital read function.
So the parameters of digitalRead functions are arduino uno
digital pins only. From 0 to 13 pins work as digital pins only.
These pins can be used digitally.
For example, to get the IR sensor value, we want to read from
the pin 7. So, the output pin of sensor is connected to pin
number 7.
digitalRead(7)
Example Code.
First we’ll connect led to pin number 13 and turn led on. I have to
use pin number 7 as an input. And we’ll connect IR sensor here.
Now IR sensor will give either 0 or 1, so we’ll store it.
int ledPin = 13;
int inPin = 7;
int val = 0;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop(){
val = digitalRead(inPin);
digitalWrite(ledPin, val);
}
So, here according to IR sensor, value wil be stored and our led
will turn on/off according to the IR sensor.
We have 3 wires here with the IR sensor. The yellow wire
indicates the 5V power supply from the Arduino. The black wire
is use to Ground. The purple wirte is actually connected to pin
number 7.
analogRead() function
So there are some devices that does not give digital values as
input. For example, potentiometer and water sensor.
So, we get analog data from these kind of sensors.
In arduino uno, we have 6 pins to read these analog signals.
But we know that microcontrollers work only on digital values,
they don’t work on analog values. So, this problem is solved by
in-built ADC design, which converts analog signals to digital
signals.
Our arduino uno is of 10 bits. So the return values will be from 0
to 1023. So 0 value means 0V and 1023 means 5V.
Syntax
analogRead(Pin)
Where Pin can be from A0 to A5
Example code
int analogPin = A0;
int val = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
val = analogRead(analogPin);
Serial.println(val);
}
analogWrite() function
If we want to change brightness of led, and change speed of our
dc motar using arduino uno then we can use this function.
We can set pin and value and it will be converted to PWM waves
before sending the signal to devices. The values for PWM waves
range from 0 to 255 only.
Syntax
analogWrite(pin, value)
We can use pin numbers 0 to 13 for analog write.
For example, for full brightness of a led, we use
analogWrite(3, 255)
Example Code
We have to change the brightness of an led light using the
analog signals of potentiometer.
int ledPin = 3;
int analogPin = A0;
int val = 0;
void setup(){
pinMode(ledPin, OUTPUT);
}
void loop(){
val = analogRead(analogPin);
analogWrite(ledPin, val);
}
map() function
We use this function to covert a number from one range to
another. For example, we can use potentiometer to change the
brightness of led, or we can control dc motar as well.
Like the way arduino converts the analog signals to digital
signals, and the 10 bit value we get ranges from 0 to 1023 value.
So this 10 bit microcontroller gives digital readings in this range
when receiving the analog signals from potentiometer.
Now we want to change the value from one range to another
range. From this digital signal values between 0 to 1023 to the
PWD waves signals that range from 0 to 255.
Syntax
map(value, fromLow, fromHigh, toLow, toHigh)
Before conversion from one range to another, we want to store
the previous value, that we store in the first parameter of map.
Now we have to specify the range of this value from low to high
in the next two parameters, that is from 0 to 1023.
Now in the last two parameters, we specify the range of the
value in which we want to convert the signals to, that is for pwd
signals, it is from 0 to 255.
Example
brightness = map(analogValue, 0, 1023, 0 , 255)
Here analogValue is the input readings from the potentiometer
In servo motar, we use angles that range from 0 to 180 value
angle = map(analogValue, 0, 1023, 0, 180)
The values between 0 to 1023 will be stored in 0 to 180
It is important to note that only pins 3, 5, 6, 9, 10, 11 can only be
connected for the PWD signals, not other pins form 0 to 13.
Example Program
int LED_PIN = 3;
int POTENTIOMETER_PIN = A0;
void setup(){
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop(){
int analogValue = analogRead(POTENTIOMETER_PIN);
int brightness = map(analogValue, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
Serial.print(“Analog ”);
Serial.print(analogValue);
Serial.print(“Brightness ”);
Serial.print(brightness);
delay(100);
}