I'm kinda new to arduino and playing around with devices.
Lately i been trying to play around with a DC bushed motor using PWM to control it, however I'm bit confused.
Is it a digital or analog signal I'm using?
This is the code to control the DC-motor to move forward
int speed = 0; // The speed
int direction= 3;
{
digitalWrite(direction, LOW); // This makes the motor drive forward
speed = 255; // The speed (Can be any value between 0-255)
analogWrite(enablePin, speed); // How long its high which is 100% due to the speed is set to 255
Because I'm pretty sure i read something about that duty cycle is using the digital signal to control how long it's high compared to low but why is it then i use analogwrite to speed and digitalwrite to direction? I made the code in more or less a gussing mode but it works fine.
Oh yeah one last thing, why is the max value of analog/digital write 255?
It is a digital signal. It is either on, or it is off.
And it is an analog signal. It contains an analog component - the duty cycle
So yes, it is an analog or a digital signal. Clear?
But seriously, it depends on how you look at the signal. From a time perspective it is purely digital. As time passes, it turns on, or it turns off. From a frequency perspective, it is an analog signal containing many frequencies of waveform.
The PWM on the arduino is generated using an 8-bit timer. This counts up from 0 to 255 internally. The value of this timer is compared with the analogWrite() value for the pin and used to decide if it should be turned on or off. As the timer can never get past 255 (all 8 bits of the counter variable turned on), there can never be a value higher than that for PWM.
Strictly speaking, everything you can do with the Arduino is digital, though it's understandable from the naming conventions that this can cause some confusion. The two functions with digital in their name, digitalWrite() and digitalRead() are only capable of manipulating 2-state (boolean/digital) results whereas the analog functions analogRead() and analogWrite() utilise the analog-digital-converter capable pins for converting an analog input to a value the microcontroller can comprehend, or simulate an analog output using PWM.
This video tutorial by Jeremy Blum has a great explanation of the relationship between analog signals, and how we use PWM to simulate them and ADC's to read them in a digital sense.
Thanks for the fast replys, I understand this alot more now.
You have all been a big help, I am almost done with my car that can follow follow 3 different colors by scanning a serial with a code on, all by using LDR.
I am not so clear on the ADC operations. I have read some stuff online and I am still confused.
When I am using a 10 bit ADC, the analog voltage suppose to be converted to a digital signal (1s and 0s) however, why does it say that the analog voltage will be represented by a value from 0 - 1023.
My idea is that this value from 0 - 1023 will really be converted to a 10 bit value? Eg. lets say the analog voltage 2.5V corresponds to 512, it(the 512) might be represented as a digital value(false but just to get the point) 0101001010 <<< and this is the digital value that we converted from the analog signal 2.5V right?
Yep that's right, except as you say, 512 isn't 010101010 or whatever, it's 1000000000 (1 and 9x 0's).
If you're a Windows user, open the built-in calculator and switch to View > Programmer, then you can easily convert between binary and decimal. If you stick in say 1111111111 (10 1's if I counted correctly) as binary, you'll see it's 1023 when you hit the decimal radio button.