Orange Pi Pulse Width Modulation

09/01/18

Last modified on 09/01/18

Categories: Single Board Computers Tags: Orange Pi

This is so cool. I had problems with a fan, which was too fast, so I was trying to figure out a way to control the speed.

The Orange Pi Zero and PC2 support PWM on pin PA06 and I found it very easy to use. evergreen-it-dev’s orangepwm library is a fork of pizypwm and is compatible with the pyA20.gpio library. Raspberry Pi Pulse Width Modulation provides a fritzing schematic showing how to test this with the orangepwm LED example script.

This is my updated code. Works like a charm, the fan runs quietly at 50% and the RetrOrangePi Zero idles at 52C and tops out around 60C, which is even lower than when the fan was running at 100%.


#!/usr/bin/python
from pyA20.gpio import gpio
from pyA20.gpio import port
from time import sleep
from orangepwm import *
import sys

FAN = port.PA6
ON_TEMP = 60
OFF_TEMP = 50
monitoring = True
fan_status = False

gpio.init()
pwm = OrangePwm(100, FAN)

def fan_shutdown():
global monitoring, fan_status
monitoring = False
fan_status = True
pwm.changeDutyCycle(0)
pwm.stop()

def fan_start():
global fan_status
fan_status = True
pwm.start(50)

def fan_stop():
global fan_status
fan_status = False
pwm.changeDutyCycle(0)

def cpu_temp_check():
try:
while monitoring:
with open( "/sys/devices/virtual/thermal/thermal_zone0/temp" ) as f:
content = f.readlines()
temp = int(content[0])
base_temp = int(str(temp)[:2])

        <span class="k">if</span> <span class="n">base_temp</span> <span class="o">&gt;=</span> <span class="n">ON_TEMP</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">fan_status</span><span class="p">:</span>
            <span class="n">fan_start</span><span class="p">()</span>

        <span class="k">elif</span> <span class="n">base_temp</span> <span class="o">&lt;</span> <span class="n">OFF_TEMP</span> <span class="ow">and</span> <span class="n">fan_status</span><span class="p">:</span>
            <span class="n">fan_stop</span><span class="p">()</span>
        <span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="k">except</span><span class="p">:</span>
    <span class="k">print</span><span class="p">(</span><span class="s2">&#34;Fan error.&#34;</span><span class="p">)</span>
    <span class="k">return</span>

if name == 'main':
cpu_temp_check()