simple PC GUI with button to LED flash

I would like to being interacting with my Arduino UNO from a PC GUI. Looking for a SIMPLE example of a button (python, excel, html or ruby)? that allows me to turn on an LED or 2 when I select the respective button.

From what I can tell the simplest way to do this is python. Unfortunately I don't know any python :(. I did install it and created the two button interface below:

import Tkinter 
from Tkinter import *
root =Tk()
root.title('Select led1')
Button(root, text='led1').pack(side= LEFT)
Button(root, text='led2').pack(side= RIGHT)

root.mainloop()

This of course has ZERO functionality! My UNO is setup with LEDs on several pins and I can get them going with physical buttons or a sketch. I'm looking for the ability to turn on the LEDs with the python interface. I'm about to start on pyserial. If anyone has some simple lines of code to turn on any pin in python with a button please sent it over.

I appreciate any direction you can offer!

thanks.

I've updated my python and arduino sketch; however, it's still not working :(. I've also tried sending ser.write('1') instead of 'HIGH'. What is interesting is that after I run my python GUI script the arduino sketcher tells me i can't upload sketches because 'COM7' is in use. This makes me believe my python script is sending serial information on 'COM7' when I select the button??? any thoughts as to what I'm missing? Again the end objective is as simple as turning an LED on when I select a button from a python GUI.

Python script

import serial
ser = serial.Serial('com7', 9600)
import Tkinter 
from Tkinter import *
root =Tk()
def sendSerial1():   
    ser.write('HIGH')
        
root.title('Select a led')
Button(root, text='led1', command = sendSerial1()).pack(side= LEFT)
Button(root, text='led2').pack(side= RIGHT)

root.mainloop()

Arduino sketch:

int led1 = 12;
int button1State = 0;


// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  Serial.begin(9600);
  pinMode(led1, OUTPUT); 
}

// the loop routine runs over and over again forever:

void  loop() {
  if (Serial.available()){
  button1State = Serial.read();
  }
  
  if (button1State == HIGH)
  {
   digitalWrite(led1, HIGH);   
  }


  
  
}

I was searching the forum for a how-to on precisely this, except I want to use VB.NET for my GUI. I finished my first semester of college recently , working towards my IT degree, and finished my Intro class to VB. Which is the reason why I want to use VB.

Twinturbotom,

So far I have gathered that we need to use the serial port on both ends in order to send and receive data and commands. I haven't any experience with using the serial ports so that is what I am trying to grasp the basics on right now.

Resources so far:

For Arduino http://arduino.cc/en/Reference/Serial http://arduino.cc/en/Tutorial/SerialCallResponseASCII http://arduino.cc/en/Reference/Stream

For .NET http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx [Added 7/6/2012] http://msdn.microsoft.com/en-us/library/y2sxhat8

In theory, (I am making a guess here) you would have Arduino somehow "listen?" (not sure if "listen" is the right term to use) for the button to be pressed? So when the button is pressed it needs to send what ever you have Arduino "listening" for through the serial port...

I only starting looking into using serial ports last night. So please if I am off in the wrong direction could some please point me in the right direction.

[so sorry for my double post]

Just want to update this thread with my own findings so that anyone else looking can take advantage of what I find as well.

I found a youtube video (http://www.youtube.com/watch?v=zLf6RfHX8sM&feature=endscreen&NR=1) with a download for sample code in the description of the video. Should give me basic understanding of how to accomplish the basics of interfacing with vb.

Downloaded the code, will dig through and test tonight and tomorrow afternoon and if successful I will try to break it down to a simpler example.

[Another Update]

Found a post on someones blog with an example done in C++ but of course the same .NET library can be used with VB.NET as well. It has example code for the PC Interface and the Arduino code… hope my findings are helpful to someone else.

Glad to hear I’m not alone. This weekend I’m going to dig into this a bit deeper. Thanks for your updates and mind share. So far I have not been able to properly send a serial signal from my python script to the arduino. I’m only using the USB, which I think should still work for this? When I click a button in my python script (the one I wish to initiate the serial data) I do not see the RX led on the arduino lighting up. I’m not sure if this is an arduino listening issue or a python script sending issue. I do know that there is a chip on the arduino board that converts serial to USB and USB to serial; I’m wondering if my “COM7” serial commands in python are not the right syntax?

You mentioned something about the arduino having to “listen”. I’ve been playing with something like this for that:

void  loop() {
  
  while (Serial.available() <=0){
   digitalWrite(led1, 0);
     delay(300);
  }
 {
   digitalWrite(led1, Serial.read()); 
  }

 
  
}

no luck yet… we’ll get it though!

Here is what I have so far. It isn't working right though.. The led turns on as soon as the port is opened with my VB app. I know I am doing it wrong, but I have more to tinker with and learn.

Arduino Code:

int led = 13;
int incomingByte = 0;

void setup() {       
   pinMode(led, OUTPUT); 
  Serial.begin(9600);  
}

void loop() {
  if (Serial.available() > 0) {
incomingByte = Serial.read();   
if ( incomingByte = 1)
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  else  
if (incomingByte = 0)  
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW                 
}}

VB.NET Code:

Imports System.IO.Ports

Public Class Form1

    Private Sub btnOn_Click(sender As System.Object, e As System.EventArgs) Handles btnOn.Click
        If SerialPort1.IsOpen = False Then
            SerialPort1.Open()
            SerialPort1.WriteLine(1)
            SerialPort1.Close()
        End If

    End Sub

    Private Sub btnOff_Click(sender As System.Object, e As System.EventArgs) Handles btnOff.Click
        If SerialPort1.IsOpen = False Then
            SerialPort1.Open()
            SerialPort1.WriteLine(0)
            SerialPort1.Close()
        End If

    End Sub

End Class

http://arduino.cc/playground/Interfacing/Python

Python is quite a cool language you can get a lot done with it easily, and there are lots of libraries to use. VB is bound to microsoft, on the positive however they got a great IDE for programming.

VB / Python well you got to start somewhere, knowing both languages might even be better i used to code a lot in python a few years ago, but if you want a GUI, buttons etc VB is simpler, i included often pygame or tinker or another one to get graphic into python.. oh well its just as hard to do real 3d graphics in VB..

PGTBOOS,

Thank you for the response. I know that if I plan on getting serious about programming I should diversify the languages I am familiar with, but at this point I try to learn enough to get by. I know Python is still widely used. So maybe it would be a good idea to pick up on it, as well as LUA.

I know what you mean about trying to use 3D in VB.NET, there is next to no example os using Direct X with VB.NET.

I'm back at it again tonight and now realize that the LED is turning on no matter what is sent to the port, but will not turn off. I just wish I could get past this point so I could move forward to sending variables back and forth. (Which I have ran into many examples of, but I'm not ready for it yet.) I have been digging through some old VB text books I have aquired over the last couple years and am surprised to find that they don't really cover serialport communication either.

From what I have gathered so far.. my Arduino code is right.. I think.. or maybe not. It will come to me eventually.

[Solved - For VB.NET]

http://www.instructables.com/id/Using-Visual-Basic-to-control-Arduino-Uno/

I have tested and ran the code myself, it works and now that I see the correct code I now understand it. Now to try and apply it in other ways.

Hopefully others will find this usefull.

Arduino Code:

//------------- START OF ARDUINO SKETCH -----------------
//
// Mixed by: Hazim Bitar
// Based on: Science Guy 14 youTube tutorial http://youtu.be/g0pSfyXOXj8

int ledPin = 13; // the number of the LED pin

void setup() {
Serial.begin(9600); // set serial speed
pinMode(ledPin, OUTPUT); // set LED as output
digitalWrite(ledPin, LOW); //turn off LED
}


void loop(){
while (Serial.available() == 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number

if (val == 1) { // test for command 1 then turn on LED
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // turn on LED
}
else if (val == 0) // test for command 0 then turn off LED
{
Serial.println("LED OFF");
digitalWrite(ledPin, LOW); // turn off LED
}
else // if not one of above command, do nothing
{
//val = val;
}
Serial.println(val);
Serial.flush(); // clear serial port
}

//------------- END OF ARDUINO SKETCH -----------------

VB.NET Code:

'------------ START OF VB 2010 CODE -----------------
' NOTE: I am using COM10 so you need to change the Visual Basic code to match your COM port

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SerialPort1.Close()
        SerialPort1.PortName = "com3" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub


    Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub


    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub
End Class


'------------ END OF VB 2010 CODE -----------------

All credit goes to the author of the article on Instructables. http://www.instructables.com/id/Using-Visual-Basic-to-control-Arduino-Uno/

This helped me a lot...

http://forums.adafruit.com/viewtopic.php?f=25&t=20177

Denis