p = (2 × AA + Aa) ÷ 2N
Count alleles, not individuals. Every homozygote carries two copies, every heterozygote carries one, and N individuals hold 2N copies in total.
q is the same with aa in place of AA, and p + q must come to 1.
Try it with your own counts
Why you count copies, not people
A population of N diploid individuals holds 2N copies of the gene. Someone who is AA contributes two A copies. Someone who is Aa contributes one A and one a. Someone who is aa contributes no A at all.
So the number of A copies is 2 × (number of AA) + (number of Aa), and the frequency is that over 2N. Using N instead of 2N in the denominator is the single most common mistake in these problems, and it shows up immediately because p and q will not sum to 1.
The square root shortcut is not general. You were probably taught q = √(aa / N). That only holds if the population is already in Hardy-Weinberg equilibrium, because it works backwards from q². In the default example above, 400 AA, 50 Aa and 50 aa, counting gives q = 0.15 while the shortcut gives 0.316. That is more than double, and nothing about the question warns you.
Then check whether it is at equilibrium
Once you have p and q from counting, you can predict what the genotype counts should be if nothing is acting on the population: p²N of AA, 2pqN of Aa, and q²N of aa. Compare those with what you actually observed.
A large gap means one of the five assumptions is broken: selection, migration, mutation, drift, or non-random mating. Since real populations always break at least one, Hardy-Weinberg is best understood as a null hypothesis you test against, not a law you expect to hold.
Common questions
How do you calculate allele frequency from genotype counts?
Count alleles, not individuals. Each homozygote carries two copies and each heterozygote carries one. So p = (2 x AA + Aa) / (2 x N) and q = (2 x aa + Aa) / (2 x N), where N is the number of individuals. The two must sum to 1, which is a free check on your arithmetic.
Can I just take the square root of the recessive frequency?
Only if the population is already in Hardy-Weinberg equilibrium. q = sqrt(q squared) assumes the genotype counts follow the equilibrium proportions. If they do not, the shortcut can be badly wrong. In the worked example on this page it gives 0.316 when the true answer is 0.15.
What is the difference between allele frequency and genotype frequency?
Allele frequency counts copies of an allele in the gene pool, so with two alleles there are 2N copies total. Genotype frequency counts individuals of each type, and there are only N of those. Mixing the two denominators is the most common error in these problems.
Why must p and q add to 1?
Because with only two alleles at a locus, every copy in the population is either one or the other. If p is 0.85 then q must be 0.15. If your numbers do not sum to 1 you have miscounted, usually by forgetting that homozygotes contribute two copies.
How do I know if a population is in Hardy-Weinberg equilibrium?
Calculate p and q by counting, then use them to predict the expected genotype counts as p squared, 2pq and q squared times N. Compare with what you actually observed, usually with a chi-square test at one degree of freedom. A large gap means something is acting on the population.
What causes a population to leave Hardy-Weinberg equilibrium?
Selection, migration, mutation, genetic drift and non-random mating, which are exactly the five things the model assumes are absent. Real populations violate at least one, so deviation is the normal case and the equilibrium is a null hypothesis rather than a description of nature.
Related tools
Hardy-Weinberg calculator, for the full equilibrium test with expected counts.
Punnett square calculator, for single crosses rather than populations.
X-linked inheritance calculator, where allele frequencies work differently in males.
Sources
The allele counting method and the Hardy-Weinberg expectations follow the standard treatment in Hartl and Clark, Principles of Population Genetics. The chi-square goodness-of-fit test for equilibrium uses one degree of freedom for a two-allele locus, because two of the three genotype classes are free once p is estimated from the data.