Skip to content

Commit 8d5a6b0

Browse files
author
Andrew
authored
Merge pull request PowerShell#55 from anmenaga/fan-example
2 parents e90823a + 030f3d0 commit 8d5a6b0

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
@{
5+
GUID="0432ee36-7e87-4a21-814f-8feb17974647"
6+
Author="Microsoft Corporation"
7+
CompanyName="Microsoft Corporation"
8+
Copyright="© Microsoft Corporation. All rights reserved."
9+
Description='PowerShell module for controling a fan over GPIO.'
10+
ModuleVersion="0.1.0"
11+
FunctionsToExport = @('Enable-Fan','Disable-Fan')
12+
CmdletsToExport = @()
13+
AliasesToExport = @()
14+
RootModule = 'Microsoft.PowerShell.IoT.Fan.psm1'
15+
NestedModules=@('Microsoft.PowerShell.IoT')
16+
HelpInfoURI = 'https://github.com/PowerShell/PowerShell-IoT'
17+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
18+
PrivateData = @{
19+
PSData = @{
20+
# Tags applied to this module. These help with module discovery in online galleries.
21+
Tags = 'IoT','RaspberryPi','Raspbian'
22+
23+
# A URL to the license for this module.
24+
LicenseUri = 'https://github.com/PowerShell/PowerShell-IoT/blob/master/LICENSE.txt'
25+
26+
# A URL to the main website for this project.
27+
ProjectUri = 'https://github.com/PowerShell/PowerShell-IoT'
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function Enable-Fan
2+
{
3+
[CmdletBinding()]
4+
param
5+
(
6+
[Parameter(Mandatory=$true, Position=0)]
7+
[ValidateNotNullOrEmpty()]
8+
[int] $Pin
9+
)
10+
11+
Set-GpioPin -Id $Pin -Value High
12+
}
13+
14+
function Disable-Fan
15+
{
16+
[CmdletBinding()]
17+
param
18+
(
19+
[Parameter(Mandatory=$true, Position=0)]
20+
[ValidateNotNullOrEmpty()]
21+
[int] $Pin
22+
)
23+
24+
Set-GpioPin -Id $Pin -Value Low
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Example module Microsoft.PowerShell.IoT.Fan
2+
3+
This PowerShell module is for turning on/off a fan on Raspberry Pi 4 case enclosure.
4+
This showcases GPIO functionality of [the Microsoft.PowerShell.IoT module](../../README.md).
5+
6+
## Hardware setup
7+
8+
[This Raspberry Pi 4 case enclosure](https://www.amazon.com/gp/product/B07XTRK8D4) comes with a 5V fan that can be connected to Raspberry Pi 5V and GND pins.
9+
This fan is nice but a little noisy so we can use this example module to turn it off when the CPU temperature is relatively low.
10+
An [IRLB8721 transistor](https://www.adafruit.com/product/355) can be used to switch power to the fan based on GPIO line of Raspberry Pi.
11+
12+
## Wiring
13+
14+
Insert IRLB8721 transistor into the break of the negative wire of the fan.
15+
Connect transistor gate to GPIO 17 (BCM schema) on Raspberry Pi.
16+
17+
![Mossfet1](https://user-images.githubusercontent.com/11860095/79925701-d6a85580-83ef-11ea-894d-93507507df5e.jpg)
18+
![Mossfet2](https://user-images.githubusercontent.com/11860095/79925725-e6c03500-83ef-11ea-9abd-7dd39be69bd1.jpg)
19+
20+
## Software setup
21+
22+
### Install PowerShell Core on Raspberry Pi
23+
24+
Installation instructions can be found [here](https://github.com/PowerShell/PowerShell/tree/master/docs/installation/linux.md#raspbian).
25+
26+
### Start Powershell and install modules
27+
28+
```powershell
29+
pwsh
30+
31+
Install-Module -Name Microsoft.PowerShell.IoT
32+
33+
git clone https://github.com/PowerShell/PowerShell-IoT.git
34+
```
35+
36+
### Usage
37+
38+
```powershell
39+
# Start monitoring CPU temperature and turn on the fan when it reaches 71 degrees; turn fan off when CPU temperature drops below 55 degrees
40+
pwsh ./PowerShell-IoT/Examples/Microsoft.PowerShell.IoT.Fan/SmartFan.ps1 -Pin 17 -OnTemperature 71 -OffTemperature 55 -TemperatureScale Celsius
41+
VERBOSE: 1:36:05 PM: CPU temperature = 71.575 C | 160.835 F
42+
VERBOSE: Starting fan...
43+
VERBOSE: 1:36:10 PM: CPU temperature = 70.601 C | 159.0818 F
44+
VERBOSE: 1:36:16 PM: CPU temperature = 70.114 C | 158.2052 F
45+
VERBOSE: 1:36:21 PM: CPU temperature = 68.653 C | 155.5754 F
46+
#...
47+
VERBOSE: 1:39:01 PM: CPU temperature = 55.504 C | 131.9072 F
48+
VERBOSE: 1:39:06 PM: CPU temperature = 55.504 C | 131.9072 F
49+
VERBOSE: 1:39:11 PM: CPU temperature = 54.043 C | 129.2774 F
50+
VERBOSE: Stopping fan...
51+
VERBOSE: 1:39:17 PM: CPU temperature = 55.991 C | 132.7838 F
52+
#...
53+
```
54+
55+
This produces following CPU temperature graph:
56+
![CPU-Temp-graph](https://user-images.githubusercontent.com/11860095/79926138-fbe99380-83f0-11ea-8705-b1336fc7bd7d.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
param
2+
(
3+
[int] $Pin = 17,
4+
[int] $OnTemperature = 70,
5+
[int] $OffTemperature = 50,
6+
[ValidateSet("Fahrenheit","Celsius")]
7+
[string]$TemperatureScale = "Celsius",
8+
[int] $PollPeriodSeconds = 5
9+
)
10+
11+
Import-Module $PSScriptRoot/Microsoft.PowerShell.IoT.Fan.psd1
12+
13+
$OnTemperatureC = $OnTemperature
14+
$OffTemperatureC = $OffTemperature
15+
if ($TemperatureScale -eq "Fahrenheit")
16+
{
17+
$OnTemperatureC = ($OnTemperature - 32) * 5 / 9
18+
$OffTemperatureC = ($OffTemperature - 32) * 5 / 9
19+
}
20+
21+
while($true)
22+
{
23+
$CpuTemperatureC = (Get-Content /sys/class/thermal/thermal_zone0/temp) / 1000
24+
$CpuTemperatureF = ($CpuTemperatureC * 9 / 5) + 32
25+
26+
(Get-Date).ToString() + ": CPU temperature = $CpuTemperatureC C | $CpuTemperatureF F" | Write-Verbose
27+
28+
if ($CpuTemperatureC -gt $OnTemperatureC)
29+
{
30+
"Starting fan..." | Write-Verbose
31+
Enable-Fan -Pin $Pin
32+
}
33+
elseif ($CpuTemperatureC -lt $OffTemperatureC)
34+
{
35+
"Stopping fan..." | Write-Verbose
36+
Disable-Fan -Pin $Pin
37+
}
38+
39+
Start-Sleep -Seconds $PollPeriodSeconds
40+
}

0 commit comments

Comments
 (0)