Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Example - managing fan of Raspberry Pi enclosure #55

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

@{
GUID="0432ee36-7e87-4a21-814f-8feb17974647"
Author="Microsoft Corporation"
CompanyName="Microsoft Corporation"
Copyright="© Microsoft Corporation. All rights reserved."
Description='PowerShell module for controling a fan over GPIO.'
ModuleVersion="0.1.0"
FunctionsToExport = @('Enable-Fan','Disable-Fan')
CmdletsToExport = @()
AliasesToExport = @()
RootModule = 'Microsoft.PowerShell.IoT.Fan.psm1'
NestedModules=@('Microsoft.PowerShell.IoT')
HelpInfoURI = 'https://github.com/PowerShell/PowerShell-IoT'
# 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.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'IoT','RaspberryPi','Raspbian'

# A URL to the license for this module.
LicenseUri = 'https://github.com/PowerShell/PowerShell-IoT/blob/master/LICENSE.txt'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/PowerShell/PowerShell-IoT'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Enable-Fan
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[int] $Pin
)

Set-GpioPin -Id $Pin -Value High
}

function Disable-Fan
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[int] $Pin
)

Set-GpioPin -Id $Pin -Value Low
}
56 changes: 56 additions & 0 deletions Examples/Microsoft.PowerShell.IoT.Fan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Example module Microsoft.PowerShell.IoT.Fan

This PowerShell module is for turning on/off a fan on Raspberry Pi 4 case enclosure.
This showcases GPIO functionality of [the Microsoft.PowerShell.IoT module](../../README.md).

## Hardware setup

[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.
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.
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.

## Wiring

Insert IRLB8721 transistor into the break of the negative wire of the fan.
Connect transistor gate to GPIO 17 (BCM schema) on Raspberry Pi.

![Mossfet1](https://user-images.githubusercontent.com/11860095/79925701-d6a85580-83ef-11ea-894d-93507507df5e.jpg)
![Mossfet2](https://user-images.githubusercontent.com/11860095/79925725-e6c03500-83ef-11ea-9abd-7dd39be69bd1.jpg)

## Software setup

### Install PowerShell Core on Raspberry Pi

Installation instructions can be found [here](https://github.com/PowerShell/PowerShell/tree/master/docs/installation/linux.md#raspbian).

### Start Powershell and install modules

```powershell
pwsh

Install-Module -Name Microsoft.PowerShell.IoT

git clone https://github.com/PowerShell/PowerShell-IoT.git
```

### Usage

```powershell
# Start monitoring CPU temperature and turn on the fan when it reaches 71 degrees; turn fan off when CPU temperature drops below 55 degrees
pwsh ./PowerShell-IoT/Examples/Microsoft.PowerShell.IoT.Fan/SmartFan.ps1 -Pin 17 -OnTemperature 71 -OffTemperature 55 -TemperatureScale Celsius
VERBOSE: 1:36:05 PM: CPU temperature = 71.575 C | 160.835 F
VERBOSE: Starting fan...
VERBOSE: 1:36:10 PM: CPU temperature = 70.601 C | 159.0818 F
VERBOSE: 1:36:16 PM: CPU temperature = 70.114 C | 158.2052 F
VERBOSE: 1:36:21 PM: CPU temperature = 68.653 C | 155.5754 F
#...
VERBOSE: 1:39:01 PM: CPU temperature = 55.504 C | 131.9072 F
VERBOSE: 1:39:06 PM: CPU temperature = 55.504 C | 131.9072 F
VERBOSE: 1:39:11 PM: CPU temperature = 54.043 C | 129.2774 F
VERBOSE: Stopping fan...
VERBOSE: 1:39:17 PM: CPU temperature = 55.991 C | 132.7838 F
#...
```

This produces following CPU temperature graph:
![CPU-Temp-graph](https://user-images.githubusercontent.com/11860095/79926138-fbe99380-83f0-11ea-8705-b1336fc7bd7d.png)
40 changes: 40 additions & 0 deletions Examples/Microsoft.PowerShell.IoT.Fan/SmartFan.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
param
(
[int] $Pin = 17,
[int] $OnTemperature = 70,
[int] $OffTemperature = 50,
[ValidateSet("Fahrenheit","Celsius")]
[string]$TemperatureScale = "Celsius",
[int] $PollPeriodSeconds = 5
)

Import-Module $PSScriptRoot/Microsoft.PowerShell.IoT.Fan.psd1

$OnTemperatureC = $OnTemperature
$OffTemperatureC = $OffTemperature
if ($TemperatureScale -eq "Fahrenheit")
{
$OnTemperatureC = ($OnTemperature - 32) * 5 / 9
$OffTemperatureC = ($OffTemperature - 32) * 5 / 9
}

while($true)
{
$CpuTemperatureC = (Get-Content /sys/class/thermal/thermal_zone0/temp) / 1000
$CpuTemperatureF = ($CpuTemperatureC * 9 / 5) + 32

(Get-Date).ToString() + ": CPU temperature = $CpuTemperatureC C | $CpuTemperatureF F" | Write-Verbose

if ($CpuTemperatureC -gt $OnTemperatureC)
{
"Starting fan..." | Write-Verbose
Enable-Fan -Pin $Pin
}
elseif ($CpuTemperatureC -lt $OffTemperatureC)
{
"Stopping fan..." | Write-Verbose
Disable-Fan -Pin $Pin
}

Start-Sleep -Seconds $PollPeriodSeconds
}