NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Python Help
March 24, 2011 by kemil |
Dear all, I am trying to create a GUI for a project i want to do. In theory its really simple, I just want to be able to change the intensity of a high powered LED using PWM by changing the value of the OCR1BL register from 0 to 255. The thing is I know absolutely nothing about python. So could anyone recommend me the following:
Many thanks Kemil |
---|---|
March 24, 2011 by Ralphxyz |
So the first question is do you have the microcontroller part using PWM and say a potentiometer working, forgetting python? Then where does the GUI come in? Do you want to have a graphical slider that you move on the pc that would brighten the led connected to the micro. There are threads here in the forum on setting up python. As far as how to setup the GUI go to the python forums. You will get other replies here but to get in depth help you probable will need to go to a python specific forum. Ralph |
March 24, 2011 by kemil |
Hi Ralph, Yes - 'a graphical slider that you move on the pc that would brighten the led connected to the micro' is exactly what im after. I am confident i have the hardware side of things set up. As im using High power LEDs i have to use some LED drivers which are connected to a power supply unit. The LED driver has a dim connection which is where the microcontroller with its PWM port comes into play. The problem is i dont know how to interface the MCU with my PC running the python GUI, (or how to make the GUI its self). How does the python script update the OCR1BL register on the MCU? i guess im looking for a blow-by-blow explanation of the interaction between the two. Thanks Kemil |
March 24, 2011 by Ralphxyz |
I think I started out with the python code for the Weight Scale and the PS2 Keyboard Tutorial. The weight scale shows you how to show a graph using Python and the PS2 Keyboard shows you how to talk to a micro using UART. I am not a Python programmer but those two projects should help you to get going. Building a GUI is a very interesting idea so please keep us posted on your progress. Ralph |
March 24, 2011 by hevans (NerdKits Staff) |
Hi Kemil, Here are a few things to get you started. Python Download site. I am pointing you directly to the python 2.6 download which is the latest version that is still compatible with PyGame PyGame download site. PyGame is a module for Python that makes it easier to write GUIs. It's one of our favorites although there are others out there. Make sure you get Python. The PyGame website has a whole bunch of tutorials that are great to get started, and their documentation is very usable. Definitely do take a look at the links Ralph pointed you to. Humberto |
March 25, 2011 by kemil |
Thanks Humberto, Can i just confirm, before i start spending time building this GUI, that it is theoretically possible to change the brightness on an LED through a GUI on my PC, whilst the microcontroller is running? Kemil |
March 25, 2011 by Rick_S |
It would be absolutely possible. The GUI on your PC would just output serial data to the micro that it would interpret to the various brightness levels. Rick |
April 05, 2011 by kemil |
Does anyone know any good tutorials on how a python script running on the pc interfaces with the microcontroller? kemil |
April 07, 2011 by kemil |
Hi all, Ive just been playing around with the tempsensor porject to get a feel for how serial connections work and have run into a problem. Using Putty (im using windows vista) I can get the temperature reading to be displayed on my PC. However when it comes to using the Python script from the meat thermometer tutorial it doesn't work properly. Im stilly trying to learn python and am not good enough yet to figure out the problem. This is what my command prompt shows when i initially run pc-pygame.py As you can see some temperature vaulus come up but the graph remains un altered. Then after about a minute this comes up on my command prompt I assume (although i have no idea) that there is something wrong in the python code beauce i can get values on putty, however, I got the python code straight from the nerdkits website and only changed the line: self.s = serial.Serial("COM4", 115200) (chenged version) As this is the port i am using and the baud rate, as stated in the servo tutorial, should be 115200. Any Ideas of what is going on anyone? Many Thanks Kemil |
April 11, 2011 by kemil |
Hi all, This is where i am up to with my little project. I have written the python code which produces a little GUI shown in the photo below. As you can see the slider goes from 0 to 255 which corresponds to the different OCR1BL levels which will determine the PWM rate and hence the brghtness of the LED. On the command prompt in the same picture you can see that i have managed to print out the values which show up on the slider. My thinking is that i could just put this variable called brightness into the piece of code:
which would port it onto the microcontroler. I got this from the web but not sure if its this simple. I am now stuck on how i am meant to set OCR1BL equal to brightness whilst the microcontroller is running. Am i meant to use an interrupt? if so how is this done? Cheers Kemil |
April 11, 2011 by hevans (NerdKits Staff) |
Hi Kemil, You have gotten pretty far, now is the time you get into the real meat of you project. Varying the brightness of an LED through PWM is not a very hard thing to do, but wrapping your head around some of the concepts can be tricky. You will have to structure your program in such a way that the main loop reads a new value from the serial port, and then simply sets the value of the PWM duty cycle right after. No interrupts should be necessary unless you want to do other things while waiting for the serial value to be read. Hope that helps. Humberto |
April 12, 2011 by kemil |
Thanks Humberto, I think my problem is i dont conceptually understand what is happening to the data as it is sent over the serial port, i.e what form is the data sent in etc. the code im am using to send and receive my data is as follows: On the python side i have the lines
which im hoping is sending the vairable 'brightness' over the serial port Whilst the code on the MCU side:
I hope is receiving this data and assigning this 'brightness' variable to OCR1BL. However, the code above doesnt work, and im not sure why not. As you can see ive set up the PWM register and declared a unsigned integer called brightness, i think i have set up the serial port with the uart_init(); command and the 2 lines underneith. Then in the while loop i use the scanf command to capture the data coming over the serial port... im pretty sure this part is wrong... but i dont know how to make it right. Anyone have anyideas of what the MCU code should look like? Thanks Kemil |
April 12, 2011 by kemil |
I have modified the C code. I can get the LED to turn on using the scroller but it does not change the intensity. new C code is: int main(void)
Kemil |
April 12, 2011 by kemil |
GOT IT TO WORK! for all those who are interested. i altered the python code bit to:
and the c code is
If anyone wants to optomise it or suggest any improvements then be my guest kemil |
April 12, 2011 by Ralphxyz |
Thanks kemil, that will come in handy one of these day. Would you post a link to your whole project or at least the basic python code to make the slider, the serial communications and then the mcu code. I do not understand your logic of passing the variable "brightness" instead just passing the brightness value but you got it to work, that's great. Ralph |
April 18, 2011 by kemil |
It is possible to send and receive data over the serial port at the same time? I want to combine the Light dimmer widget with the temp sensor project.. but this would required me to send data to the MCU whilst also reading from it.. Anyone know how i should go about doing this? Kemil |
April 18, 2011 by Ralphxyz |
Isn't that what USART does? or is it UART that does the simultaneous communications. Ralph |
April 18, 2011 by hevans (NerdKits Staff) |
H Kemil, Ralph is right, you should be able to read and write through the serial port at the same time with no problem. On the computer end you will have to make sure the same process is doing both the reading and the writing as only program can keep the device open at the same time, but other than that it should be straight forward. Humberto |
May 05, 2011 by kemil |
Hi Humberto, When u guys say use USART instead does this mean i have to do something similar to what u have done with UART in the libnerdkits folder? has anyone writtten a function similar to that of uart_init() but for USART... even once reading the data sheet i dont know what bits on what registers to turn on and off in order to do what i want. Thanks Kemill |
May 05, 2011 by kemil |
Just tried it with the uart_init for libnerdkits and it works. but i have a bigger problem Im using a modified version of the tempsensor code where ive just added an if statement which turns on an led if the temp is above a certain value. Ive written a short python code which allows me to change the minimum temperature which turns on the led: the python code is:
This kind of worked but it wouldnt give me the continuous stream of data which i was looking for (i was able to change the data_inp_1 value in the if statement: if (temp_avg > data_inp_1 ) though, which is good) ... instead it only gave me the temp reading when i pressed the button.... i think this is because the print command is only called when the button is pushed.... so i tried moving the ser.readline and the print x commands but doing this made the gui not even come up! I looked through the pc-pygame python code for the meat thermom project and it uses the threading module... im not sure how to implement this module and i have i hunch its what i need to do, i tried playing around with it to no avail.... Does anyone know what i need to do to be able to print out continuouse stream of temperatures? Thanks Kemill |
May 05, 2011 by kemil |
UPDATE: I think ive narrowed down the problem... its def something to do wtih my code, shown below: i have changed my python code so i just print out the temp values. This ONLY works successfully when i comment out the scanf_P line in my code. when i uncomment it and runt the same code it doesnt work. The question is how do i simaltaneously send and receive data from the mcu.. whos got the code to do so? Thanks Kemill
|
May 05, 2011 by hevans (NerdKits Staff) |
Hi kemil, scanf is a blocking I/O call. Which means the code will wait to read something before moving on. This is probably what is causing your confusion. What you can probably do is use the uart_char_is_waiting() function of uart.c to check if there is anything waiting on the serial port, and only call scanf if there is something to read. Humberto |
May 09, 2011 by kemil |
Thanks Humberto, That Solved one of the problems but now i have another! As previously mentioned i want to be able to send commands to the mcu whilst at the same time get temp readings from it. The thing is i cant seem to get a continuous stream of data from it unless i use an infinate while loop and it seems that if i do that i can no change the temperature range(i have a little function which just turns on an LED if i the temperature is above a certain value which i can adjust through my small gui). I need to be able to continuously read the serial data whilst doing other things. Thanks Kemil Here's my python code:
|
Please log in to post a reply.
Did you know that multiple microcontrollers can communicate with each other? Learn more...
|