Variable PWM problems

I’ve made a basic pulse monitor using the FEZ. However I’m having an issue altering the PWM setting via external commands. The problem I am experiencing is that in order for the system to be compatible with many users I need to alter the power sent to the photo-transistor; more sensitive for bigger fingers etc. To do this I alter the PWM by taking a value sent from the UART Com Port, from an external program. This is done within a perpetual loop [while(true)]. When I attempt to send data to the FEZ, the data, being transmitted back through the port, locks up; then stutters and dumps out larger chunks of data. Oh, and the TEST LED doesn’t respond to the new values.

Any ideas?

Much Thanks in advance.


I read the post twice and didn’t understand what you are doing and what the question is! :slight_smile:

What UART has to do with PWM and where the chunks cam from? :slight_smile:

HERE’s the extract of what I wrote in chat… I hope this is clearer.

I’m having a little difficulty adjusting a PWM pin externally on the fly. The FEZ seems to become stuttered in sending out data to the UART. I’m controlling the output of a PWM pin through Matlab; at least i’m trying to - among other things. I have a small array being sent continuously to the UART and read in correctly in Matlab. I can control the PWM on set-up correctly but… What i am trying to do is send data Back through the UART (to the FEZ) from Matlab to alter the PWM value as it is running. The readings I get from An0 are determined by the level of sensitivity from a phototransistor. So if the readings are not as clear I need to adjust the voltage to said phototransistor and the reading should alter accordingly. The system seems to chock and become stuttered whenever I send data back through the UART. I can communicate perfectly with Matlab.

I was able to send data cleanly and get the FEZ to repeat the data back whilst adding more info…
“Data received : #####” etc. It does blow-out chunks on data intermittently kind of all stuttered But not uniformly as I had expected and also Matlab complains that the bit terminator times out too

        public static void setLED(String volts)
            byte x = byte.Parse(volts);

                if (x > 0 & x < 100)

                    IR_LED.Set(10000, x);

… this is the function that is called from within a while loop.

in Matlab I have

if ~strcmp(LED, oldLED)
            fprintf(FEZPort, ['' num2str(LED)])
            oldLED = LED;

it clams “Warning: A timeout occured before the Terminator was reached”. Now normally this means that the bitstop is never rached but… this only happens when i try to alter the PWM
It works fine when I dont alter the PWM on the fly
I alter the PWM with the Matlab code above the fprintf command sends data to the COM port; called FEZPort in this case. It sends a string holding the numerical values.
on the FEZ side I have:

read_count = UART.Read(rx_byte, 0, 1);
                if (read_count > 0)// do we have data
                    string cs = "You typed: " + rx_byte[0].ToString() + "\r\n";
                    byte[] buffer2 = Encoding.UTF8.GetBytes(cs);
                    UART.Write(buffer2, 0, buffer.Length);

the setLED then runs the first code I pasted

Can you please wrap your code in code tags? It would make it a lot easier to understand.

Sorry TopCat, you need to make this more clear. You get free support but please some efforts to explain would be helpful :slight_smile:

Also, like Chris said, tag the code so it is readable…thanks

Sorry Gus… I edited the above post. I hope it makes a little sense. :-[

In a nutshell, I’m trying to alter the PWM setting by receiving data from the COM port on the FEZ. Com port communication works perfectly with Matlab both ways but as soon as I try to adjust the PWM setting, by sending the FEZ values, it causes problems in reading from the device.

Reading from what device? What is reading from what?


TopCat… let us know if this understanding is correct so far.

  1. You’re communicating to Fez via the UART com port.

  2. You’re using MathLab to send the data to the Fez

  3. You’re trying to set the voltage of a Photo Transistor by sending it pulses from one of the PWM pins.

Oh dear, I am in a muddle here, I have a pounding headache :frowning:

Okay let me try and start again…
I am communicating with a FEZ Domino through Matlab. I successfully read and write data to and from the FEZ Domino through the UART port, in Matlab; using Matlabs serial port functions.

I successfully control the illumination of an LED using the PWM command and all is well. I successfully send data (a small array holding 2 integers) from the FEZ Domino to Matlab perpetually in a while loop. This is read continuously in Matlab without any issues and its displayed on a nice little graph.

I have an LED (or phototransistor) that I would like to adjust its voltage value on the fly, by sending new values to the FEZ Domino from Matlab. I have written some code in both Matlab and on the FEZ Domino (C#) to cater for this. See some extracts above:

The problem I have is this:
When I try to alter the PWM values from Matlab and continue to read the stream of data, the data stream seems to lockup. And then randomly spits out chunks of data and locks again. The LED does not change either.

It could be that I am very tired and have a headache…

I hope this is clearer, sorry if it isnt :frowning:



TopCat… let us know if this understanding is correct so far.

  1. You’re communicating to Fez via the UART com port.

  2. You’re using MathLab to send the data to the Fez

  3. You’re trying to set the voltage of a Photo Transistor by sending it pulses from one of the PWM pins[/quote]

Yes this is correct… :slight_smile: the pulses on the PWM pins need to be altered from the data sent to the Fez, from Matlab.

Just as an additional note…

Matlab is set to send and receive data asynchronously. I have tested this by sending and receiving data perfectly, to and from the Fez… The issue only reveals itself when I try and alter the PWM values. The FEZ seem to lock up the port stream…

I don’t think the issue is with Matlab. That’s why I needed to ask for help.



How much data are you sending? Maybe you are overflowing the USBizi UART buffer with data?

Make matlab wait for a an okay from USBizi before it sends more data…(handshaking)

Hmmm… It may well be… I’ll do that and let you know. Thank you!

Oh, It only sends two integers at a time (as a String) repeatedly.

The system I’m developing needs to be very fast. I am reading data from both an EEG neural headset over blue-tooth and the FEZ Domino. On a negative note the Domino (at full pelt) is sending data at half the speed of the headset :frowning: so it is proving to be the bottleneck in the system; thus the reason I had the Domino sending data in an un-paused stream. When it had a pause of 1/100th of a second it was over 6 times slower than the headset.

This is just a minor issue but, might well be what’s causing the problem :frowning:

Thanks again…


Then you need to put the load on your PC not on FEZ. For example, you need to send numbers over UART not strings! Then use this to extract the integer Utility.ExtractValueFromArray(…);
We also have one that extract floats…

…now that is embedded programming :slight_smile:

…no strings …no serialization… :wink:

Great advice Guz, as usual.

Yeah, I have decided to put an old fashioned variable resistor to control the load, and away from the FEZ; I really can’t lose any more speed at this point.

Thanks again everybody.


Perhaps using the Analog Output port to set the voltage to the Photo Transistor? Just a thought

Yes, I would have though of this but… :’(

Apparently not something you should do :expressionless:


Using an AnalogOut is fine if you have very low current requirements. Driving a transistor is a low current application.

Lighting a LED is a high current requirement for a DAC.

Or you can add an op-amp but I usually do not recommend this since most hobbyists do not know what is an op-amp and how it works :slight_smile:

Yeah an op-amp would work but adds unnecessary complexity to the system. I’m just as happy at this point to slap in a manual variable resistor and simply read in through the analogue in pins.

Thanks again for your help guys. And thanks for being understanding to a very tired man yesterday. I could have fell asleep at my desk if my desk wasn’t so close to my bed! :smiley: