I have own a Microchip 16f877a PIC and a controller board for a while but never really use it in projects or even put effort on learning it. The reason is simple, the IDE provided with the controller kit, MPLAB can only be executed on MS Windows which i really hate using apart from gaming (until few days ago i only notice that MPLAB X IDE is a free cross platform IDE, but it’s too late, i already in love with Arduino..).

 

 

Until 1 or 2 weeks ago, thanks to David, i heard the name “Arduino”. After google around, i found that the Arduino IDE is available for most of the linux distro and all you have to do is installing the arduino package, that’s easy, isn’t it? To my surprise, the language to code the board is C++ which is my favourite, at that time, i already fall in love with Arduino before ever getting a grip on the hardware.

Finally, i get a chance to play with the Arduino Nano board. After uploading a couple of example sketches on to the board, i decided to try something different, which is the serial communication between the board and my laptop. Therefore, i decided to do a simple (very simple indeed) project which is controlling a LED’s brightness using the software in my laptop.

Below is the wiring schematic for this micro project. Note that we did not connect any resistor to the digital port 9 simply because an internal resistor, R9 already existed in the board.


The hardware wiring is ready so let’s get to the board programming part, below is the code:


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

void loop()
{
  if(Serial.available())
  {
    int brightness = (int)Serial.read();
    int value = map(brightness,0,127,0,255);
    analogWrite(9,value);
  }
}

Inside the setup() function, the Serial.begin(9600) is initialise the  serial I/O with a baudrate of 9600. Then the pin9 is identified as the output pin. For the loop() function, we check that if the serial data is available, we read the serial data and cast it into integer and assign to the variable birghtness. After that, we map the brightness value (0-127) to the PWM value (0-255) and send out a PWM signal. Next, by using PyQt4 and PySerial, we wrote a GUI program which have a slider.

 


from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
import serial
 
def printNumber(number):
    print number
    ser = serial.Serial('/dev/ttyUSB0', 9600)
    ser.write(unichr(number))

 
if __name__=="__main__":
    #First we create a QApplication and QSlider
    app=QApplication(sys.argv)
    slider=QSlider(Qt.Horizontal)
    slider.setRange(0,127)
    slider.setFixedWidth(400)
 
    QObject.connect(slider,SIGNAL("valueChanged(int)"),printNumber)
 
 
    slider.show()
    #Start the evnt loop
    sys.exit(app.exec_())

 

 

The coding is also simple, we create a application with QApplication and a QSlider component as its main widget which have a range from 0-127. Then we use signal and slot to call the printNumber function once the slider’s value changed. The printNumber function will print the slider’s value on terminal and create a Serial instance which will then send out the value.

Since Serial.write() function can only send string or characters, the value is first converted to ASCII code and then later reverted back to integer by the Arduino board.

That’s it, we have a powerful open source project board, Arduino. A powerful all rounder language Python as backend and a brilliant frontend, Qt4. We can now control the LED brighness by moving the slider. Although this micro project is very simple, but i get a lot of fun playing with it and gaining new knowledge (Python and Arduino). I will using it in the future project and playing around with it when free. Stay tuned for more interesting projects 🙂