# How to determine the horizontal pixel count out of a HSync and VSync signal

I have a project where I want to read the HSync and VSync signal from a VGA cable into an microcontroller and calculate the pixel clock.

Calculation: Pixel clock = Horizontal Pixel Count * Vertical Line Count * Refresh rate

I'm currently stuck by the question of how I can get the horizonal pixel count.

If we assume that my VGA cable caries a 640x480 (800x525 with the porches) @ 60Hz signal, like shown here:

I can get the refresh rate by timing the vertical sync pulses to each other (~16.68ms > 1/16.68ms = 60Hz.) I can get the vertical pixel count by dividing the time of every frame and the time of every HSync pulse to each other (16.68ms / 31.77µs = ~525 lines).

How do I get the horizontal pixel count?

For 640x480 @ 60Hz it should be 800 pixels.

Really important is that the resolution and framerate of the VGA cable is not fixed, so I can not use any definitions for timings.

For clarification: I know that a VGA cable sends analog signals and there aren't any pixels. But my source device have pixels and they convert them into analog video signals. So want to get the pixel clock from my source device that is connected via a VGA cable.

• no, for an VGA 640x480 signal 640 are only the visible pixels. The other 160 pixels are in the front and backporch and in the sync pulse.
– user321329
Commented Jan 13, 2023 at 12:52
• But, isn't it an analogue signal and you can sample it at whatever rate you want? Commented Jan 13, 2023 at 13:04
• Since VGA is an analog interface I don't think it has a (digital) pixel clock. Instead you get an analog waveform with continuously distributed (analog) voltage. Use a digital interface if you need a pixel clock, or make up your own after you convert to digital. Commented Jan 13, 2023 at 13:59
• I just realized you said you already knew there were no pixels. If you know there are no pixels why are you asking how many pixels there are? There are none. Zip. Zilch. Zero. Commented Jan 13, 2023 at 14:10
• That can't be calculated since there isn't enough information, but you could definitely try to estimate it via Fourier analysis and from the vertical refresh. You'll get it wrong occasionally but for most devices it shouldn't be too hard to correctly guess. Commented Jan 13, 2023 at 14:24

You could find out what is the display formats of the signal by analyzing the time between VSYNCs and between HSYNC.

I have found a table in the datasheet of the AD9888. At page 15 they give the expected timing on theses signals versus the display formats.

There you will also find the pixel clock rate for that given format.

Thus you need to implement a lookup table with these values in your code, measure the HSYNC and VSYNC timing to know the format and fetch the corresponding pixel clock from the table.

• That might be the best option to have lookup table. And based on the timings to choose the most likely resolution
– user321329
Commented Jan 13, 2023 at 14:11
• @ArtjomEske you did say you can't use any definitions, does that include guessing the best definition out of a big table of definitions? Commented Jan 13, 2023 at 14:40
• Since 2 answer have already stated that it’s not that easy or not possible since there isn’t enough data to calculate it, the best option would be to look in a lookup table to estimate the resolution from what I’m measuring. I said that in don’t want any definitions but a lookup list would be the most possible way and only estimating the pixel count I a fine trade off.
– user321329
Commented Jan 13, 2023 at 14:50

There are no horizontal pixels in VGA.

Remember VGA comes from the era of CRT (cathode ray tube) monitors. The electron beam (cathode ray) sweeps from left to right across the screen, and the VGA signal continuously adjusts the brightness of the beam. There are no pixels.