Adjust brightness of Arduino's built-in LED with pyFirmata - led

I'm learning to control an Arduino using Python with pyFirmata.
I can turn the LED on and off, but how do I adjust the brightness?

Related

How to control the rate of LED blinking

I am using STM32 board to control the color and blinking rate of the RGB LED. For varying the color of RGB LED I have configured a timer in PWM mode and by varying the duty cycle of three signals on three channels of this timer, the LED changes the color. How can I control the blinking rate of LED with another timer? Which mode of second timer and technique should be used to control the on and off time?
Thanks in advance.
There are many ways that you can do this.
The simplest is in the main loop of your application to read some clock or free running timer. If it is time for the next LED colour then update the PWM duty cycle.
A more complicated approach that does not require code in the main loop would use a DMA whose source is an array of PWM values and whose destination is the duty cycle register of the output timer. You can then use a different timer to periodically trigger the DMA.

How can one achieve an arbitrary frequency and duty cycle PWM-ed IR transmission in an xBee3 using MicroPython?

I am researching a way to send out a PWM-ed IR pulse from an xBee3 device. I couldn't find any IR related libs, so I guess I'll have to "brute" force it by "waiting" X many microseconds and setting digital I/O pins ON and OFF.
Any ideas/pointers/references would be much appreciated!
MicroPython on the XBee3 RF products won't be capable of the microsecond timing you're looking for, and the PWM output only allows for setting the duty cycle, not the frequency.
You would need to add extra hardware to convert (for example) the I2C interface to IR transmission. The XBee3 RF products don't support a secondary UART like the XBee Cellular products do, but if they add that feature you would could have a UART->IR interface via a secondary microprocessor.

Configure external clock for atmega 1281 on proteus

Creating a stopwatch and want to measure the time more accurately, no matter what frequency is used(8MHz. 4MHz,2MHz,1MHz) in the internal oscillator, there are always errors with the counting time depending on the frequency (10-20 seconds delay)
already connected the 8MHz crystal, 2, 20pF capacitors to XTAL1, and XTAL2 ports on proteus board but have no idea about how to configure the fuses thing in avr coding in C
What do you mean by Proteus Board? If you are referring to Proteus Simulation, then you don't have to program the fuse bits in the micro-controller under the simulation environment. But you can choose the Clock type (External/Internal) and provide the frequency value by right clicking on the microcontroller and then click on properties. If you want to set the fuse bits in actual hardware/PCB mounted MCU, let me know the IDE you are using.

HW Timer (PWM) or SW Timer to control a LED

As the title says, is it generally good practice to use General Purpose Timers for dimming a LED (PWM with variable duty cycle) or is it better to use OS scheduling/tasks when available (RTOS ecc)?
I recently saw an example of a blinking led using the RTOS internal timers and i was wondering if the period of the timer can be fastened up to the point where you can dim a led (~2Khz).
Regards,
Pulsing a LED in software could flicker if some other task were to interfere with its scheduling, and you won't get much fine control over brightness. So if PWM hardware is available (and it can work with that pin, and isn't needed for something else), I would use the hardware.
A common pattern is to use PWM to control the visible brightness of the LED, then to have a regularly scheduled sofware task to vary it smoothly (to produce fades, blinks and so forth), based on a counter and some state/variables which might be controlled by other tasks.

Generating a tone with PWM signal to a speaker on a PIC32 microcontroller

I'm currently working on generating a tone on a PIC32 device. The information I've found has not been enough to give me a complete understanding of how to achieve this. As I understand it a PWM signal sends 1's and 0's with specified duty cycle and frequency such that it's possible to make something rotate in a certain speed for example. But that to generate a tone this is not enough. I'm primarily focusing on the following two links to create the code:
http://umassamherstm5.org/tech-tutorials/pic32-tutorials/pic32mx220-tutorials/pwm
http://www.mikroe.com/chapters/view/54/chapter-6-output-compare-module/#ch6.4
And also the relevant parts in the reference manual.
One of the links states that to play audio it's necessary to use the timer interrupts. How should these be used? Is it necessary to compute the value of the wave with for example a sine function and then combine this with the timer interrupts to define the duty cycle after each interrupt flag?
The end result will be a program that responds to button presses and plays sounds. If a low pass filter is necessary this will be implemented as well.
If you're using PWM to simulate a DAC and output arbitrary audio (for a simple and dirty tone of a given frequency you don't need this complexity), you want to take audio samples (PCM) and convert them each into the respective duty cycle.
Reasonable audio begins at sample rates of 8KHz (POTS). So, for every (every 1/8000th of second) sample you'll need to change the duty cycle. And you want these changes to be regular as irregularities will contribute to audible distortions. So you can program a timer to generate interrupts at 8KHz rate and in the ISR change the duty cycle according to the new audio sample value (this ISR has to read the samples from memory, unless they form a simple pattern and may be computed on the fly).
When you change the duty cycle at a rate of 8KHz you generate a periodic wave at the frequency of 4KHz. This is very well audible. Filtering it well in analogue circuitry without affecting the sound that you want to hear may not be a very easy thing to do (sharp LPF filters are tricky/expensive, cheap filters are poor). Instead you can up the sample rate to either above twice what the speaker can produce (or the human ear can hear) or at least well above the maximum frequency that you want to produce (in this latter case a cheap analogue filter can help rid the unwanted periodic wave without much effect on what you want to hear, you don't need as much sharpness here).
Be warned, if the sample rate is higher than that of your audio file, you'll need a proper upsampler/sample-rate converter. Also remember that raising the sample rate will raise CPU utilization (ISR invoked more times per second, plus sample rate conversion, unless your audio is pre-converted) and power consumption.
[I've done this before on my PC's speaker, but it's now ruined, thanks to SMM/SMIs used by the BIOS and the chipset.]
For playing simple tones trough PWM you first need a driver circuit since the PIC cannot drive a speaker directly. Typically a push-pull is used as actively driving both high and low results in better speaker response. It also allows for a series capacitor, acting as a simple high-pass filter to protect the speaker from long DC periods.
This, for example, should work: http://3.bp.blogspot.com/-FFBftqQ0o8c/Tb3x2ouLV1I/AAAAAAAABIA/FFmW9Xdwzec/s400/sound.png
(source: http://electro-mcu-stuff.blogspot.be/ )
The PIC32 has hardware PWM that you can program to generate PWM at a specific frequency and duty cycle. The PWM frequency controls the tone, thus by changing the PWM frequency at intervals you can play simple music. The duty cycle affects the volume, but not linearly. High duty cycles come very close to pure DC and will be cut off by the capacitor, low duty cycles may be inaudible. Some experimentation is in order.
The link mentions timer interrupts because they are not talking about playing simple notes but using PWM + a low pass filter as a simple DAC to play real audio. In this case timer interrupts would be used to update the duty cycle with the next PCM sample to be played at regular intervals (the sampling rate).

Resources