Analog and Digital Signals

In this post we are going discuss something which is used in all the electronics and computers which is the Pulse Width Modulation and the concepts related to it.

Logic level is the voltage level that a particular device communicates with. A common logic level that a micro-controller uses is 5V. An analog varies from 0V to 5V for example as a sine wave. So, at any point of time we can get any value between 0 and 5 volts in the case of the analog signal. On the other hand digital signal can have only two values 0V or 5V just like the on or off.

Analog to Digital
Digital signals are more efficient since the analog signals have a lot of noise in them. So, we usually convert the analog to digital. The analog to digital converter on the Arduino is of 10-bit which means we have 2^10 = 1024 resolutions. Thus the analogRead() reads the analog value from 0 to 5 volts and assigns it a digital value from 0 to 1023. Whereas the digitalRead() reads either high (1) or low (0). The analogWrite() is 8-bit resolution and takes values from 0 to 255.


Digital to Analog (PWM)
The conversion of the digital to the analog is done using the pulse width modulation (PWM) . To do so we modulate the digital signal at a particular duty cycle. Since the device takes time to respond, it averages out the signal and we get the analog output. LEDs respond very quickly to the changing voltage but when we modulate the LED, its our eyes which average out the signal since they are no t fast enough to discern the difference.

Hence by changing the time that  signal is on versus the time that the signal is off , we can simulate a range of voltages between o and 5 volts. The duty cycle represents how much of the time is the signal high in one cycle. It is a ratio of the time that the signal is high to the total time in one cycle.The output voltage is determined by multiplying the maximum voltage ( usually 5V ) and the duty cycle. For example the duty cycle of 50% gives us the output analog voltage of 2.5V which is analogWrite(127).

Duty cycle frequency is the number of duty cycles that occurs every second. For example if there is one duty cycle in one second we have frequency 1Hz. Here is how the brightness of a LED varies as the duty cycle changes.


Bit-banging PWM 
We can even manually implement the PWM on any digital pin by turning it on and off. We make use of our knowledge that delay() function maintains the current state for a milli-second i.e. delay(1000) delays for 1 second.

Following is a code for the generation of 25% duty cycle @ 10Hz
 
void setup() {

  pinMode(13,OUTPUT);
}

void loop() {

  digitalWrite(13,HIGH);
  delay(25);
  digitalWrite(13,LOW);
  delay(75);
}

Food for thought : The analog reading ( value 0-1023 ) is taken from the analog pin of the Arduino. Whereas the digital reading ( 0 or 1 ) can be taken from any of the pin. The digitalWrite( high or low ) can be given from any of the pins but the analogWrite (0-255) can only be given from the PWM pins !

Comments

Popular posts from this blog

The move_base ROS node

Three Wheeled Omnidirectional Robot : Motion Analysis

Overview of ATmega328P