0

I'm a newbie in Arduino. I find out a couple of examples.

Here, below arduino simple code I just uploaded.

Here, what is functionality or working of pinMode, digitalWrite and digitalRead ?

const int kPinLed = 7;
const int kPinBtn = 8;

void setup()
{
  pinMode(kPinLed, OUTPUT);
  pinMode(kPinBtn, INPUT_PULLUP);
}

void loop()
{
  if(digitalRead(kPinBtn) == LOW)
  {
    digitalWrite(kPinLed, HIGH); 
  }
  else digitalWrite(kPinLed, LOW);
}
5
5
{
  pinMode(kPinLed, OUTPUT);
  pinMode(kPinBtn, INPUT_PULLUP);
}

This block of code used to define mode of Pin i.e. making pin as Output pin or Input pin.

if(digitalRead(kPinBtn) == LOW)
{

}

This block of code is used to read value from defined digital pin either High or Low. And in return will give pin state High or Low. For example if we want to make LED ON which is connected to one of digital pin of Arduino at certain condition, we will define that pin under above block of code and can read state of that pin as High or Low.

 {
   digitalWrite(kPinLed, HIGH); 
 }
 else digitalWrite(kPinLed, LOW);

This block of code is used to make Pin state High or Low i.e. Under certain condition this function will make pin state either High or Low.

To learn more, visit......

PinMode Click here

DigitalRead Click here

Digital Write Click here

5

Welcome to the world of Arduino!

To start off, the Arduino website has excellent descriptions of its various API functions, in your case - https://www.arduino.cc/en/Reference/pinMode

https://www.arduino.cc/en/Reference/DigitalWrite

https://www.arduino.cc/en/Reference/DigitalWrite

Now to answer your question:

pinMode determines whether a specified pin behaves as input or output - does it read a signal to the Arduino (read from a a sensor, for example) or does it output to another component (a led to light it, a motor to move it). It achieves this by changing the impedance on the pin- high impedance for input, low for output.

IINPUT_PULLUP means both setting the pin to input mode, while 'pulling up' the input to be high when it is not explicitly low by an external component (whatever is connected to the pin)

  //configure the pin somePin to behave as output
  pinMode(somePin, OUTPUT);  
  //configured the pin to behave as input, and 'pullup' the input to HIGH 
  pinMode(somePin, INPUT_PULLUP); 

Once you have set a pins mode to be input/output, you can read or write from it, depending on its mode.

If you configured a pin to be in input mode, you can use digitalRead

//read the value from someInputPin and store it in the variable 'num'
int num = digitalRead(someInputPin);

If you configured a pin to be in output mode , you can use digialWrite

//write the value from 'num' to someOutputPin, and by extension whatever connected to it
int num = HIGH;
digitalWrite(someOutputPin,num);
3

Very good question for every beginner. Let me explain.

pinMode() : This function set the functionality of pin. Means either INPUT or OUTPUT or INPUT_PULLUP.

INPUT use when you connect your arduino to the sensor.

OUTPUT use when you connect your arduino to LED, motor etc.

INPUT_PULLUP use when we require internal pull-up. For example, if we connect a pushbutton to our arduino then we require some pull-up resistor. Then this thing can be done by this INPUT_PULLUP. So we don't require any resistor in circuitry.

Now depend upon your circuit requirement you have to use proper pinMode.

Syntax: pinMode(pin, mode)

Where,

pin: the number of the pin whose mode you wish to set

mode: INPUT, OUTPUT, or INPUT_PULLUP.

You can see this thing in your code.

void setup()
{
  pinMode(kPinLed, OUTPUT);       // LED as OUTPUT
  pinMode(kPinBtn, INPUT_PULLUP); // Push Button as INPUT with pull-up.
}

digitalWrite(): This function set pin states either HIGH or LOW. Here, 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.

Syntax: digitalWrite(pin, value)

Where,

pin: the pin number

value: HIGH or LOW

Now, again in your code.

digitalWrite(kPinLed, HIGH); // Pin set as HIGH

digitalWrite(kPinLed, LOW);  // Pin set as LOW

digitalRead(): This function use to read the value from a specified digital pin, either HIGH or LOW.

Syntax: digitalRead(pin)

Where,

pin: the number of the digital pin you want to read (int)

And this thing in your code looks like.

if(digitalRead(kPinBtn) == LOW) // Check pin states : LOW or HIGH
{
  //Your code...
}
2

PinMode sets the mode or function of the pin, like input (when you connect it to a sensor), or output (when you connect it to a LED for example). There are also some variances (like pullup/pulldown for special cases).

In practice, when you set the value to LOW, 0 V will be put on the signal, and 5 V will be put when it is HIGH (at least on an Uno).

PinModes are normally set during setup (once), and DigitalRead/Writes in the loop since you want to change it dynamically.

Btw, the last pat of the example can be better written as:

digitalWrite(kPinLed, digitalRead(kPinBtn) == LOW ? HIGH : LOW);

The ? operator (called ternary operator) checksk if the condition (digitalRead(kPinBtwn) == LOW is true, if so it will use the value behind ? otherwise the vvalue after : is used).

1
  • 1
    digitalWrite(kPinLed, !digitalRead(kPinBtn)); works also fine. – Mikael Patel Mar 18 '17 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.