使用Digispark Attiny85构建Raspberry Pi电源(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/pi/build-a-raspberry-pi-powersupply-with-digispark-at-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2365 个词 阅读量 0使用Digispark Attiny85构建Raspberry Pi电源(译文)
原文地址:https://www.codeproject.com/Articles/1105987/Build-a-Raspberry-Pi-powersupply-with-Digispark-At
原文作者:Andrew Lai
译文由本站 robot-v1.0 翻译
前言
Power on, safe shutdown and power cut-off for Raspberry Pi
Raspberry Pi的开机,安全关机和断电
介绍(Introduction)
Raspberry Pi是一款了不起的设备,它以如此低的成本将如此多的功率打包成一个小巧的外形.(Raspberry Pi is an amazing device, packing so much power into a small form factor with such a cheap cost.) 不幸的是,它没有自带电源.关机包括运行shutdown命令并拔下USB电源.(Unfortunately, it does not comes with its own power supply. Shutting down involves running the shutdown command and unplugging the USB power.) 可以找到实现的视频演示(A video demonstration of the implementation can be found) 这里(here) .(.)
设计(Design)
该电源包括:(This power supply consists of:)
-
按钮-触发开,关和强制关机功能(Push Button - To trigger on, off and forced power off function)
-
LED灯-显示状态:ON(亮起),OFF(闪烁),OFF(灭)(LED light - To show status of, ON (light up), Turning off (blinking), OFF (light off))
-
中继-使用Attiny85(5V USB电源)将电源驱动到Pi(Relay - Driving the power into the Pi using Attiny85 (5V USB power))
-
晶体管x 2(2N2222或任何类似的NPN晶体管)(Transistors x 2 (2N2222 or any similar NPN Transistor))
- 一个用于驱动继电器向Pi供电的继电器(One for driving the Relay to feed power into Pi)
- 用于触发Pi的关闭命令的示例(在此示例中使用GPIO12/Pin32)(One for trigging the shutdown command of Pi (using GPIO12 / Pin32 in this example))
-
Attiny85(我使用Digispark USB开发板进行开发)-主控制器(Attiny85 (I used Digispark USB development board for development) - Main controller) 交换机的工作方式如下:(Here’s how the switch is designed to work:) 接通电源后,attiny85上电,继电器处于OFF状态,Pi没有上电. attiny85正在等待按钮被按下.(When power is connected, the attiny85 is powered up, relay is in OFF state and Pi is NOT powered. The attiny85 is waiting for button to be pressed.)
-
按下按钮后,继电器接通. Pi通电并启动.(When button is pressed, relay is switched on. Pi is powered and boots up.)
-
再次按下按钮时,程序会向Pi发送信号以使其关闭. Pi具有运行和侦听GPIO端口上的信号的python脚本.收到信号后,Pi将运行关机命令.(When button is pressed again, the program sends a signal to Pi to shutdown. The Pi has a python script running and listening to signal on the GPIO port. When signal is received, Pi will run shutdown command.)
-
程序等待直到通过监视GPIO14(UART TX)(电压<0.1)将Pi完全关闭.(The program waits until Pi is fully shutdown by monitoring GPIO14 (UART TX) (voltage <0.1).)
-
一旦确认Pi完全关闭,程序将关闭继电器,Pi的电源将被切断.(Once confirmed Pi is fully shutdown, the program turns the relay to OFF and Pi’s power will be cut.)
电路原理图(Circuit Diagram)
- 电路图也附:(Circuit diagram also attached:)
- digispark的P0驱动继电器为Pi供电(P0 of digispark drives the relay to power to Pi)
- digispark的P1驱动状态LED(P1 of digispark drives the status LED)
- digispark的P2从按钮输入进行电源开/关按钮(P2 of digispark takes input from button for power on / off button)
- digispark的P3通过GPIO12触发Pi的关闭(P3 of digispark trigger shutdown of Pi via GPIO12)
- digispark的P4检测是否通过GPIO14(UART TX)关闭了Pi(P4 of digispark detects if Pi is shutdown via GPIO14 (UART TX))
使用代码(Using the Code)
-
将草图安装到attiny85中.我正在使用Digispark USB开发板((Install the sketch into attiny85. I am using Digispark USB development board ()
sketch_pi_switch.ino
)())- 这是一个(Here’s a) 入门指南(starting guide) 前往digispark.(to digispark.)
-
在Raspberry Pi中运行关闭脚本((Run the shutdown script in Raspberry Pi ()ShutdownDetect.py(ShutdownDetect.py))())
兴趣点(Points of Interest)
int SHUTDOWN_PIN = 3;
int RELAY_PIN = 0;
int BUTTON_PIN = 2;
int LED_PIN = 1;
的(The) powerstatus
变量用于跟踪pi的电源状态.(variable is used to keep track of the power status of pi.)
int powerstatus = 0;
//Power Toggle Mode
// 0 Power off
// 1 Powering On (not used for now)
// 2 Powered on
// 3 Powering off
// 4 Waiting for Safe Shutdown to finish
// 5 Hard Shutdown
每个循环都会延迟50毫秒,从而使计时变得容易一些(例如,按下按钮的时间).(Each loop is delayed by 50ms, making timing a bit easy (e.g., how long the button has been pressed).) 如果长时间按下按钮(60 * 50ms)=3s,则强制关闭电源.(If button is pressed for a long time (60 * 50ms) = 3s, force to power down.)
if(buttondowncount>=60 && buttonupcount >2)
{//Long Pressed Button
buttondowncount = 0;
buttonupcount = 0;
powerstatus = 5;
}
如果短时间按下按钮(3 * 50ms)=150ms.从电源关闭((If button is pressed for a short time (3 * 50ms) = 150ms. Either move from power off () powerstatus
0到1)或开始关机顺序((0 to 1) or start power off sequence () powerstatus
2至3).(2 to 3).)
else if(buttondowncount>=3 && buttonupcount >2)
{//Button Pressed
buttondowncount = 0;
buttonupcount = 0;
if(powerstatus==0)
powerstatus=2;
else if(powerstatus==2)
powerstatus=3;
}
代码的下一部分是每个动作的位置(The next section of the code is where the actions are for each) powerstatus
.(.)
//Switch on relay to power on
if(powerstatus==2)
{
digitalWrite(SHUTDOWN_PIN, LOW);
digitalWrite(LED_PIN, HIGH);
digitalWrite(RELAY_PIN, HIGH);
}
else if(powerstatus==3)
{
//Start Shutdown Sequence
//Trigger shutdown script in Pi
digitalWrite(SHUTDOWN_PIN, HIGH);
delay(100);
digitalWrite(SHUTDOWN_PIN, LOW);
powerstatus=4;
checkOffCount = 0;
}
else if(powerstatus==4)
{
//Waiting for fully power off signal in TX pin
//Blink LED to signal powering off
digitalWrite(LED_PIN, LOW);
delay(300);
digitalWrite(LED_PIN, HIGH);
delay(200);
//Check if it's off 5 times before shutting down
if(checkPiOff())
checkOffCount = checkOffCount + 1;
else
checkOffCount=0;
if(checkOffCount==10)
{
//It's really off
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
powerstatus = 0;
checkOffCount = 0;
}
}
else if(powerstatus==5) //Hard shutdown
{
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
powerstatus = 0;
checkOffCount = 0;
}
最有趣的部分是(The most interesting section would be) powerstatus==3
和(and) 4
.下面触发pi并运行shutdown命令并将powerstatus设置为(. Below triggers the pi and runs the shutdown command and sets the powerstatus to) 4
(等待安全关机完成).((waiting for Safe Shutdown to finish).)
digitalWrite(SHUTDOWN_PIN, HIGH);
delay(100);
digitalWrite(SHUTDOWN_PIN, LOW);
powerstatus=4;
然后在(Then in) powerstatus == 4
,它使LED灯闪烁并监视pi是否完全处于关闭状态.(, it blinks the LED light and monitors whether the pi is fully in shutdown state.)
else if(powerstatus==4)
{
digitalWrite(LED_PIN, LOW);
delay(300);
digitalWrite(LED_PIN, HIGH);
delay(200);
//Check if it's off 5 times before shutting down
if(checkPiOff())
checkOffCount = checkOffCount + 1;
else
checkOffCount=0;
if(checkOffCount==5)
{
//It's really off
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
powerstatus = 0;
checkOffCount = 0;
}
}
兴趣点(Points of Interest)
- 即使关闭了PI,Digispark也会消耗一些功率.我正在1Mhz模式下运行它,以便消耗最低功率.(Digispark does draw some power, even when the PI is shutdown. I am running it in 1Mhz mode so that it draws the minimum power.)
- 可以轻松地改进该项目,以通过红外线远程控制Pi的电源.让我知道你的想法!(This project can be easily improved to remotely control the Pi’s power via Infrared. Let me know your thoughts!)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
Python Raspberry Arduino 新闻 翻译