
What does analogRead(arg1) function do?įigure-2: ADC Module of ATmega328P Microcontroller The PWM signal is being generated by 16-bit TC1 and the resolution of this PWM signal is 7.97 us.Ģ. The ON-period is determined by arg2 whose value is limited to 0 - 255 (0x00 - 0xFF). If we execute analogWrite(9, 0x20), the PWM signal of shape B (Fig-1) will be generated at DPin-3 with ON-period = 2040/2 8*0x20 = 255 us. The PWM signal of the context is being generated by the 8-bit TC0 (Timer/Counter-0). arg2 determines the minimum time (the resolution) by which the ON-period of the PWM signal can be augmented. The ON-period of the signal is determined by arg2 whose range is limited to (0 - 255 (0x00 - 0xFF). When analogWrite(3, 0x20) is executed, the PWM signal of shape A (Fig-1) is generated at DPin-3 of UNO. What does analogWrite(arg1, arg2) function do?įigure-1: PWM signals generated by analogWrite() function

Here, we have three functions: ananlogRead(arg1), map(arg1, arg2, arg3, arg4, arg5), and analgWrite(ar1, arg2) these three functions can be combined as follows to do a new thing - what is this new thing? analogWrite(arg1, map(ananlogRead(arg1), 0, 1023, 0, 255)) ġ. I am a beginner in the arduino started kit but I've been playing around with map function and it seems that th analogRead can get values raging from 0- 1023 but whenever I want to do an analogWrite this should be from 0 - 255 why aren't both of them the same range? The map function can also be used for that, but it's more suited to change the range, e.g. Or, more advanced, chopping off two bits with "bitshift". That can simply be done by dividing by four. We must convert 0-1023 coming from analogRead to 0-255 for PWM, or 10-bit binary to 8-bit binary. So if we control a motor with a pot connected to the analogue input, for PWM output.Įnough resolution for most things, like controlling LED brightness or motor speed. The digitalWrite part of the processor is 8-bit, so it only has 256 steps (0-255) e.g. We can measure a voltage difference of 5volt/1024steps = ~5millivolt per step/level. So with the 5volt power supply basic Arduino processor are running on, Representing 0-1023 decimal (1024 steps/levels of accuracy). The analogRead part of basic Arduinos has a 10-bit resolution, You need to understand that to fully understand the difference between 8-bit and 10-bit. Lots of information about the binary number system online. There are ten digits needed to write "1023". It therefore needs more digits to write "255", actually eight (11111111).


Only three digits are needed, because each digit itself can have ten different 'levels (0-9).Ī computer only works with yes/no, on/off, 1/0, so only two levels per digit. With the decimal system you're used to you can write the number 255 with three digits.
