C/C++ Program to blink an LED WITHOUT Arduino, WITHOUT Microcontroller

This is a C, C++ console application that uses the DTR output of a serial port to toggle an LED on and off from a console application. Detailed explanation and the circuit diagram is given in the appended youtube video.

Categories | About |     |  

Parveen,
  1. You will need a 470 ohm resistor, an LED of any color and an FTDI USB to serial module. We have tested the program on Windows 10 - no drivers were required. Please refer the appended video for all the details.
  2. Compile the program using visual studio - any version will be OK.
  3. Assemble the circuit on a bread-board or on a PCB and plug it ON.
  4. The COM port number can be obtained from the device manager.
  5. Run the program!
IMPORTANT NOTE: ONLY FOR WINDOWS. NEEDS A COMPILER WITH WINDOWS SET UP. COMPILE USING Visual Studio C++, Visual C++ Express, any edition, any version. WARNING!!! DATA ON THE PEN DRIVE WILL BE REMOVED.

The source code

This is the program with all necessary comments -

#include <iostream>

#include <windows.h>

using namespace std;

// COMPILE WITH A WINDOWS COMPILER 
// SUCH AS VISUAL STUDIO C++ 2005 
// OR LATER. PROGRAM TESTED ON 
// WINDOWS 10, 8 and 7 
#define ON  SETDTR

#define OFF CLRDTR

int main()
{

  // see the video below on how to 
  // get the port number from 
  // device manager 
  printf("COM Port number[1-9]: ");

  // path format \.\COM1 
  char path[] = "\\.\\COMx";

  cin >> path[6];

  HANDLE hComm = CreateFileA(
  path,
  GENERIC_READ | GENERIC_WRITE,
  0,
  0,
  OPEN_EXISTING,
  NULL,
  0);

  if (hComm == INVALID_HANDLE_VALUE)
  {

    printf("Error. Check COM Port?");

    return -1;

  }

  // user input key 
  char key = 0;

  // state of the LED 
  int state = OFF;

  do 
  {

    // toggle the state of the LED 
    state = (ON == state) ? OFF : ON;

    // turn the DTR high/low 
    EscapeCommFunction(hComm, state);

    // ask the user to enter 
    // any key to toggle state 
    // q to quit 
    cout << "Enter any key to";

    cout << " toggle LED [q to quit]:";

    cin >> key;

    cout << "LED turned ";

    cout << (ON == state ? "ON":"OFF");

    cout << endl;

  } while ('q' != key);

  // close the COM handle 
  CloseHandle(hComm);

  return 0;

}

Circuit diagram and Discussion

Please refer the video below for discussion and circuit diagram -


This Blog Post/Article "C/C++ Program to blink an LED WITHOUT Arduino, WITHOUT Microcontroller" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Comments and Discussion