Arduino Code for 0-500MHz, 1nW to 100W RF Power Meter

Here is the code for the Arduino in-line power meter that will feature in my Data Modes column in the August PW.
Copy and paste the code below into your Arduino IDE:
 --------------------------------------------------------------------------------------------------------------------------
 /*
 This sketch uses the Adafruit i2c/SPI LCD backpack
 and shares some code from Adafruit
 ( http://www.ladyada.net/products/i2cspilcdbackpack/index.html )
Sketch produced by Mike Richards (G4WNC) for publication in the August issue
 of Practical Wireless magazine in the UK
 www.g4wnc.com
The LCD Backpack connections:
 * 5V to Arduino 5V pin
 * GND to Arduino GND pin
 * CLK to Analog #5
 * DAT to Analog #4
* The AD8307 voltage output connects to the Arduino A0 pin
*/
// include the library code:
 #include "Wire.h"
 #include "LiquidCrystal.h"
// Initialise the variables
 int value = 0; // Used to store the raw reading from the ADC
 float vout = 0.000; // Holds the true value of the ADC output voltage
 float powerdB = 0.00; // Calculated power in dBm
 float pWatts = 0.00; // Calculated power in watts
 float slope = 40; // Slope of the AD8307 log output (Default = 40)
 float intercept = 44.0; // 0V intercept point (Default = 44)
 float refVolts = 2.5; // Measured value of the 2.5V external reference
// Connect to the LCD via i2c, default address #0 (A0-A2 not jumpered)
 LiquidCrystal lcd(0);
void setup() {
 // Start by setting-up the pin configurations
 analogReference(EXTERNAL); // Set the Arduino to use an external reference for the ADC
 pinMode(4, OUTPUT); // Digital pin 4 is used to supply power to the voltage reference
 digitalWrite(4, HIGH); // Make sure pin 4 is high
 pinMode(A0, INPUT); // Enable the first (A0) input to the ADC
 // set up the LCD's number of rows and columns:
 lcd.begin(20, 4); // Set 4 lines of 20 characters
 // Print a message to the LCD.
 lcd.setCursor(0,0); // set the cursor to the top left, line 0 column 0
 lcd.print("Arduino Power Meter"); // Message on the top line of the display
 lcd.setBacklight(HIGH); // Turn the backlight on to make the display visible
 //Now print the measurement labels
 lcd.setCursor(0,1); // move the cursor to the first position on the 2nd line
 lcd.print("AD8307 Volts: "); // print the label
 lcd.setCursor(0, 2); // Move the cursor to the start of the 3rd line
 lcd.print("Power (dBm): "); // Print the label
 lcd.setCursor(0, 3); // Position the cursor
 lcd.print("Power (Watts):"); // Print the label
 }
void loop() {
value = analogRead(0); //read the ADC and store the result in value
 vout = (value*refVolts)/1023; // Convert the ADC result to volts in vout
 powerdB = (slope*vout)-intercept; // convert the voltage to dBm in 50 ohms
 pWatts = pow(10.0,(powerdB -30)/10.0); // convert dBm to watts
 lcd.setCursor(13, 1); //Move the cursor to the 13th position
 lcd.print(vout,3); // Display the AD8307 voltage
 lcd.setCursor(13, 2); // Position the cursor
 lcd.print(" "); // Print spaces to clear the last result
 lcd.setCursor(13, 2); // Position the cursor
 lcd.print(powerdB,1);// Display the power in dBm
 lcd.setCursor(15,3); // Position the cursor
 lcd.print(" "); // Print spaces to clear the last result
 lcd.setCursor(15,3); //Position the cursor
 lcd.print(pWatts,1);// Display the power in watts
 }

8 Responses to Arduino Code for 0-500MHz, 1nW to 100W RF Power Meter

  1. Lee Layland July 3, 2015 at 7:30 pm #

    A word of warning when copying this code – the Arduino software doesn’t like the ” marks when they are copied, you need to replace all of them by physically typing the ” into the program.

    • admin July 3, 2015 at 11:03 pm #

      Hi Lee – Thanks for spotting that problem. I’ve had a look at the problems and the web formatting on my post was using 66 and 99 style inverted commas. I’ve now changed the page formatting so it’s safe to copy and paste direct from the web.

      Thanks again, Mike G4WNC

      • Lee Layland July 3, 2015 at 11:10 pm #

        Thanks for that. It took me a while to figure out what was going wrong!

        A very informative article BTW, thank you. I look forward to reading you again very soon.

        73 de M0LGL

  2. Lee Layland July 29, 2015 at 3:03 pm #

    You need the new LiquidCrystal Library from Adafruit, and the:
    #include “Wire.h”
    #include “LiquidCrystal.h”
    needs changing to
    #include
    #include

    73 de M0LGL

  3. Lee Layland July 29, 2015 at 3:04 pm #

    ‘ #include ‘
    ‘ #include ‘

    • Lee Layland July 29, 2015 at 3:05 pm #

      Arrrrrgh – it won’t let me put the code in – remove the ” and rep[lace with

      • Lee Layland July 29, 2015 at 3:06 pm #

        Greater than and less than signs – sorry but every time It wouldn’t let me post the actual symbols

  4. arjit August 20, 2017 at 12:50 pm #

    In can measure Frequency in kHz to MHz ???? Insted db

Leave a Reply

Powered by WordPress. Designed by WooThemes