Hey guys! I am completely new to Arduino, and am trying to understand the use of PinMode in setup. I was using the example sketch for AnalogInOutSerial, and I can't see why pin 9 is not declared with PinMode (9, OUTPUT) in setup. I somewhat understand how pin 9 is being used for PWM to create an average voltage, but I fail to understand why PinMode isn't used. It is a very basic question but I'd appreciate some guidance as I am trying to make a simple light sensor.
Any pin used by analogWrite is automatically (intrinsically!) an output, so no need to set the pinMode.
Same as there is no need to set the mode of a pin used by analogRead.
The Analog Read Serial example MIGHT be a better place to start (with your light sensor in place of the potentiometer). I shows the actual analog readings on your computer so you can see what you're working with.
So why do we need to set the pinMode() of a pin used for digitalWrite() ? It is, after all, obviously being used as an output ?
... 9600 baud is ALWAYS used?
Because doing digitalWrite to an input pin simply switches the input pullup on or off.
I know why, but following your logic the chip is intrinsically using the pin as an output just as when you use analogWrite() on an analogue pin
Of course, to confuse things you can use the A* pins as digital pins
Hey! I don't make the cores.
It was the completeness of your reply that I was referring to rather than the sometimes convoluted logic of the CPU core that I was referring to
The analogWrite() function automatically sets the pin to OUTPUT.
From //arduino-1.8.5/hardware/arduino/avr/cores/arduino/wiring_analog.c:
void analogWrite(uint8_t pin, int val)
{
// We need to make sure the PWM output is enabled for those pins
// that support it, as we turn it off when digitally reading or
// writing with them. Also, make sure the pin is in output mode
// for consistenty with Wiring, which doesn't require a pinMode
// call for the analog output pins.
pinMode(pin, OUTPUT); //<<<<<<<<<<<<<<<<<<<
I mean I would also like to understand that in more detail!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.