Hi, I just want to ask the difference between analogWrite and digitalRead
As stated in the arduino website, digitalRead reads the value from a specified digital pin, either HIGH or LOW.
As for analogWrite it is written that an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin).
Can someone explain to me in laymen terms on the difference between the both functions?
Thank you!
digitalRead() does what it says. It reads the state of a pin and returns a value, either HIGH or LOW, that reflects the state of the pin.
analogWrite() causes a voltage between 0V and 5V to be written to a pin. The voltage is turned on and off continuously and the ratio between the on and off is set by the second parameter used in the analogWrite(). As a result the voltage at the pin is a square wave. When connected to say an LED, the brightness of the LED will vary depending on the on/off ratio. If the voltage is at 5V for most of the time then the LED will be lit brightly. On the other hand if the voltage is at 0V for most of the time the LED will be lit dimly.
Note that analogWrite() can only be used with PWM enabled pins (marked with a ~) on the Arduino and that the actual voltage output voltage is always 0V or 5V. analogWrite() does not actually vary the voltage.