Akash Yanakandla
Published © GPL3+

Control Room Lamp via HC-05 Using.NET WinForms

Wirelessly control a lamp in your room using a C#.NET WinForms app and an HC-05 Bluetooth module—simple, smart, and fun!

ExpertFull instructions provided1 hour74
Control Room Lamp via HC-05 Using.NET WinForms

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
High-Power Relay Shield for Particle Photon I²C 1-Channel SPDT 10-Amp
ControlEverything.com High-Power Relay Shield for Particle Photon I²C 1-Channel SPDT 10-Amp
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Visual Studio 2017
Microsoft Visual Studio 2017
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver

Story

Read more

Schematics

The image

Code

Arduino Nano Code

Arduino
Serial.begin(9600); — Starts serial communication at 9600 baud rate.

pinMode(2, OUTPUT); — Sets digital pin 2 as an output (to control LED or relay).

In the loop(), the Arduino listens for serial input:

If it receives '1', it sets pin 2 HIGH (turns ON the LED/relay).

If it receives '0', it sets pin 2 LOW (turns OFF the LED/relay).
void setup() {
  pinMode(2, OUTPUT);  // Built-in LED
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    char command = Serial.read();
    if (command == '1') {
      digitalWrite(2, HIGH); // Turn ON LED
    } else if (command == '0') {
      digitalWrite(2, LOW);  // Turn OFF LED
    }
  }
}

.NET Window Forms

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using System.Windows.Forms;
//using System;
using System.IO.Ports;
using System.Windows.Forms;

namespace WinFormLed
{
    public partial class Form1 : Form
    {
        SerialPort serialPort = new SerialPort();
        public Form1()
        {
            InitializeComponent();
            serialPort.PortName = "COM9";
            serialPort.BaudRate = 9600;

            try
            {
                serialPort.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening COM9: " + ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort.IsOpen)
                serialPort.Close();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (serialPort.IsOpen)
            {
                serialPort.Write("1"); // Turn ON LED
                //MessageBox.Show("Sent '1' to COM9"); // Debug line
                MessageBox.Show("BULB IS TURNED OFF, DEAR AKASH YANAKANDLA");
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (serialPort.IsOpen)
            {
                serialPort.Write("0"); // Turn OFF LED
                //MessageBox.Show("Sent '0' to COM9"); // Debug line
                MessageBox.Show("BULB IS TURNED ON, DEAR AKASH YANAKANDLA");
            }
        }
    }
}

Credits

Akash Yanakandla
2 projects • 0 followers

Comments