What is the exact frequency of PWM signals produced from Arduino pins - arduino

In http://arduino.cc/en/Reference/analogWrite it says,
" The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz. "
I want now to check that frequency. Simply I typed this command analogWrite(9,127), which will generate PWM signal with duty cycle 50%.
Then I used the pulseIn built-in Arduino function (pulseIn descriotion) and typed this command pulseIn(9,HIGH) to measre the period in which the signal is HIGH. It returns in average 1010 us.
So the period time is 2020 (1010 for the High period and 1010 for the low period) and consequently the PWM frequency from that pin is 495.04 ~ 495Hz.
The same is done with pin number 5. The High period is 506 uS, The full period is 1012 uS, The PWM frequency from that pin is 988.14 Hz ~ 988 Hz.
So, What is the exact PWM frequency ?!
The following pictures is snapshots for the Arduino sketch and the results
The Arduino sketch
When I pressed '1' multiple times
When I pressed '0' multiple times

PWM signals are created by timers. A timer increments a register, and various methods create a square signal based on that.
The frequency depends on 3 things:
the master clock: 8MHz or 16MHz usually on Arduino)
the prescale (or divisor on the page below): to slow down the frequency, you can setup the timer to increase the count every 1, 2, 4, 8, 16… master clock cycles
the maximum count: there are several counting methods, but an important limit is the register size (usually 8 bits and 16 bits on Arduino)
You'll find specific details on this page:
http://playground.arduino.cc/Main/TimerPWMCheatsheet

So, What is the exact PWM frequency ?!
imo There are to many variables to give the "exact" PWM frequency between boards and other factors.
For example, if I were to test two different boards from the same company I would not be able to get an exact match.

You've used one of the two pins where the PWM signal works at 980hz!
More specifically the pin 5 and 6!
This is what the website of Arduino says:
The frequency of the PWM signal on most pins is approximately 490 Hz.
On the Uno and similar boards, pins 5 and 6 have a frequency of
approximately 980 Hz.
http://arduino.cc/en/Reference/analogWrite

Related

Arduino Leonardo and Funduino Mega, frequency on analog read

I write a simple Arduino software where I read some information on analog pins.
I compile this software on an Arduino Leonardo and on a Funduino Mega. Anyway, the Arduino obtains a better sampling frequency. Did you think that I can improve the frequency performance if I buy an Arduino Mega?
The main difference is using on-chip USB interface on Arduino Leonardo. You can use baud rate settings, but it'll be ignored and it'll run as fast as it's possible.
On the other side, if you're using Mega 2560 with Serial.begin(9600); and for example Serial.println(analogRead(A0));, then it's not possible to get about theoretical 15000 samples per second as you can send only about 960 characters per second.
Btw: for three 8bit characters (for HEX representation, 4 characters for DEC representation), "\r\n" separator and 15000 samples/s you'll need baud rate about: 5*(1+8+1)*15000 = 750000 baud/s (8bit characters + 1 start bit + 1 stop bit)

Accelerometer data: How to interpret? Sparkfun ADXL335 with Arduino

X:471 Y:486 Z:476
X:468 Y:478 Z:467
X:454 Y:460 Z:450
X:436 Y:435 Z:422
X:392 Y:379 Z:364
X:327 Y:305 Z:296
X:270 Y:248 Z:250
X:248 Y:236 Z:246
X:260 Y:258 Z:269
X:292 Y:297 Z:307
X:321 Y:331 Z:341
X:375 Y:398 Z:406
X:439 Y:465 Z:465
X:478 Y:502 Z:494
X:489 Y:503 Z:491
X:478 Y:487 Z:475
X:462 Y:465 Z:451
X:413 Y:401 Z:385
X:343 Y:321 Z:313
X:272 Y:247 Z:249
X:253 Y:239 Z:248
X:260 Y:256 Z:268
X:288 Y:293 Z:304
X:320 Y:330 Z:340
X:374 Y:394 Z:400
X:436 Y:464 Z:463
Here is my sample of data (Not moving at all) I don't know what can i do with all these data.
I wrote a blog post about this device on an Arduino that should answer your questions.
http://chrisheydrick.com/2015/02/05/adxl335-accelerometer-on-an-arduino/
In short, the values you're seeing depend on the power delivered to the ADXl335.
The sensitivity (mV/g) is stated to be ratiometric in the data sheet, and the example given is that when you deliver 3V to the power supply, the sensitivity is 300 mV/g. In a later section called “Use with operating voltages other than 3V” it gives the example of 360 mV/g with a 3.6V power supply, and 195 mV/g with a 2V power supply. You can pretty much gather that the sensitivity in mV/g is the power supply voltage divided by 10. More or less.
Another ratiometric value is the “0g bias”. The accelerometer chip can detect negative acceleration, but it doesn’t output a negative voltage signal. What you do is consider the middle point of the power supply voltage range the 0 point. My power supply voltage is 3.3V, so I have to treat 1.6V as the zero point. You’ll add have to subtract that zero point from any voltage reading to get the actual mV/g value.

Why arduino voltage read is higher when there is relay turned on (a load on a digital pin)

I am using arduino Uno to read voltage from A0 pin with simple 1/3 resister divider (5v applies to a voltage divider, and A0 gets 1/3 of 5v). There is a relay connected to D1 pin.
(My eventual goal is to measure 15v, that's why using voltage divider to diagnose the problem)
I noticed that when relay is on, the A0 read is higher than it should be. I am not sure what's the cause, and like to understand it.
Description about the circuit:
A0: analog pin used to measure voltage.
D1: a digital pin used to control a relay.
Resistor divider: R1 = 2k, R2 = 1k. R1 connects to 5v (arudino Uno 5v output). A0 is connected to R2, therefore, A0 should get 1/3 of 5v, which is 1.67v.
Few measurements to diagnose the problem:
Vmesaure_all: a manual voltage measure on R1 + R2.
Vmeasure_r1: a manual voltage measure on R2 only, which is input voltage of A0.
Vcode_r1_A0: arduino A0 analog read.
A)Relay is off:
Vmeasure_r1=1.67v (1.67 *3 = 5.01)
Vmesaure_all=5.03v
Vcode_r1_A0=339 (339 * 3 = 1017)
All above makes sense.
B) Relay is on:
Vmeasure_r1=1.63v (1.63 * 3 = 4.89v. OK. Makes sense as it is almost same as Vmesaure_all)
Vmesaure_all=4.91v (Relay is a load, it makes voltage drop on if measuring voltage of R1+ R2. I think it is expected)
Vcode_r1_A0=345 (why higher than 339, which is relay off A0 read???)
I could not explain this. If use this value to calculate voltage on R1 + R2, you will get higher voltage (the voltage to be measured), compared with relay off.
I would expect Vcode_r1_A0 to be < 339.
I did few experiments, andgot consistent behavior described above. Why?
Yes, the numbers can make sense. The analog to digital converter (ADC) by itself is not an absolute voltmeter. ADC's have a reference voltage and are calculating the digital value relative to that reference voltage. The Arduino provides you choice of reference voltage via the function analogReference()
The default behavior is that analog reference is the supply voltage pin, which is nominally 5.0 V. But that leads to your problem:
If Vcc changes while the analog input stays the same, the digital value changes.
If Vcc drops, the digital value will increase.
In other words, an ADC is only as accurate as its reference voltage.
The ATmega chips include a voltage reference that will not vary with Vcc. If you set the INTERNAL reference, then full scale 1024 digital is now 1.1 V.
analogReference(INTERNAL);
x = analogRead(A0);
// now x = 1024 is A0 = 1.1 V
Using this code, the digital value will be more accurate and not sensitive to Vcc. The tradeoff is that the full scale range is greatly reduced to only 1.1 V. To accommodate that reduced range, you need to add a voltage divider. Because you already intend to do that, you should use the internal reference and adjust the resistor values of your divider.
But, you are dropping Vcc by 0.1 V! Do you notice the chip is hot or a burning smell? Confirm that the current drawn by the relay coil is within the rating of the pin. Commonly, people use a transistor or driver chip (for example ULN2003) to power a relay load. Also beware that connecting inductive loads like relays and motors straight to digital pins tends to destroy those pins because of the flyback voltage when you turn off the coil.

Shift register m74hc165 connection

I am following the following tutorial (http://www.arduino.cc/en/Tutorial/ShiftIn) to connect a shift-in register to my Arduino. In the tutorial they use a CD4021 'CD4021' shift register and I got a 'm74hc165'. After looking at the datasheet (http://www.datasheetlib.com/datasheet/735260/m74hc165b1_sgs-thomson-microelectronics.html#datasheet) I cannot figure out how to connect the following wires:
Q8 (pin 3) to Ardunio DigitalPin 9 (blue wire)
CLOCK (pin 10) to to Ardunio DigitalPin 7 (yellow wire)
P/S C (pin 9) to Ardunio DigitalPin 8 (green wire)
Mentioned in the tutorial in section 2. Can somebody help me with how to connect these three wires to the shift-register?
Several years later, but hope it helps.
According to the data sheets, the 74165 has the following equivalences to the CD4021:
Q8 would be Qh (pin 9) in the 165. The 4021 has three outputs for the last three stages of the shift register. The 165 has only one: Qh.
CLOCK would be CLK (pin 2) in the 165. But CLK INH must be low (wired to GND). This is used to inhibit the clock, something we don't want in most of the setups, including the Arduino example.
P/S C would be SH/^LD (pin 1). But caution. In the 165, the logic is inverted. A HIGH value means the data can be shifted, and a LOW value the parallel data must be latched. The CD4021 is the other way around: HIGH means latch the parallel data and LOW let the bits shift serially.
All this obtained from this more recent data sheet.

I need help reading analog voltages in an arduino mega2560

I'm new to microcontrollers and I need help reading voltages and printing them in the serial monitor. I have done this using a potentiometer, but I was wondering if I could do the same with just voltages. Below is the code that I used when I read the potentiometer values:
I have tried reading voltages but when I input a voltage smaller than between 3-5V in pin A0 I get 0s in the serial monitor
int potPin = 0; // select the input pin
int val = 0; // variable to store the value coming from the function generator
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
val = analogRead(potPin); // read the value from the function generator
Serial.println(val);
delay(1000); //Sampling rate for the signal
}
as Udo Klein said, this looks like a hardware issue. try setting up your circuit like this: http://imgur.com/PYbeqLq
note that the grounds are shared between the function generator and the arduino - this is important for ensuring that voltages are measured correctly. remember that voltage is actually a difference in electric potential, so you need a common reference point to measure from.
To answer your question, yes you can read voltages from another piece of hardware.
As gearbot tells you it is important to keep a common ground between the arduino and the other piece of hardware.
Also be carefull, the arduino can only measure voltages between 0 and 5 Volts. Don't try to measure higher voltages without adjusting the circuitry as you might destroy your arduino board.
If you're unsure measure the voltages before attaching to the arduino with a multimeter.
The Uno has 6 analog inputs, labeled A0 through A5, each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and the analogReference() function.
edit:
I didn't see the added comment at the bottom of your post. This indicates indeed a hardware problem (probably in your circuitry)
To be sure the arduino does the right thing, measure the voltage with your multimeter between pin A0 and GND while your hardware is attached and working. You should be able to read the correct voltage there.
I suspect it will give the same values as the Arduino.
Then trace back your circuitry with your multimeter (keeping GND attached) to find where something is going wrong.
Try scaling your 0 to 5 or 1-5v input
using map function.
int setValue = map(ana1Value, 0, 1023, 0, 100);
could be a help for you.
the analogue input voltage is the scaled 0-100 then you can work with that

Resources