Raspberry Pi 2 と WebIOPi を使って A/D コンバーター経由でアナログ値を取得してみた - しばやん雑記 ã§ã¯ WebIOPi ã使ã£ã¦ A/D ã³ã³ãã¼ã¿ã¼ã® MCP3002 ã使ãã¾ããããRaspberry# IO ããåãããã«æ±ãæ¹æ³ããã£ã¨åãã£ãã®ã§æ¸ãã¾ãã
çµè«ããæ¸ã㨠MCP3004/3008 㨠MCP3002 ã¯ä»æ§ãå¾®å¦ã«ç°ãªããããRaspberry# IO ã«ç¨æããã¦ããã¯ã©ã¹ã§ã¯å¯¾å¿åºæ¥ãªãã¨ãã話ã§ããããã¼ã¿ã·ã¼ããå¼ã£å¼µã£ã¦ãã¾ããã
å¾®å¦ã« Channel Selection ã®æå®ãç°ãªã£ã¦ãããã¨ãåããã¾ãããã㦠MCP3008 㯠D2 ãããã使ãããã«ãªãã ãã¨ãã£ãéãã«ãªãã¾ããã ããå ±éååºæ¥ããã§ããã
ã©ããããããªãã®ã§ããã©ã¼ã¯ã㦠MCP3002 åãã®ã¯ã©ã¹ã追å ãã¦è§£æ±ºãããã¨ã«ãã¾ããã
ä¸å¿ãæ¬å®¶ã«ã Pull Request ãæãã¦ããã®ã§ãéãè¯ããã°ãã¼ã¸ãããããããã¾ããã
ä»åã® MCP3002 ç¨ã¯ã©ã¹ã追å ãã Raspberry# IO ã使ãã°ã以ä¸ã®ããã«æ¸ãã¾ãã
using System; using System.Threading; using Raspberry.IO.Components.Converters.Mcp3002; using Raspberry.IO.GeneralPurpose; namespace ConsoleApplication1 { class Program { static void Main() { var adcClock = ProcessorPin.Pin11; var adcMiso = ProcessorPin.Pin09; var adcMosi = ProcessorPin.Pin10; var adcCs = ProcessorPin.Pin08; var driver = new GpioConnectionDriver(); using (var adcConnection = new Mcp3002SpiConnection(driver.Out(adcClock), driver.Out(adcCs), driver.In(adcMiso), driver.Out(adcMosi))) { var ch0 = adcConnection.In(Mcp3002Channel.Channel0); var ch1 = adcConnection.In(Mcp3002Channel.Channel1); while (!Console.KeyAvailable) { Console.WriteLine("ch0: {0}", (int)ch0.Read().Value); Console.WriteLine("ch1: {0}", (int)ch1.Read().Value); Thread.Sleep(1000); } } } } }
ã¡ãªã¿ã«ã³ã³ãã¤ã«ããã«ã¯ä»¥ä¸ã®ããã« mcs ãå©ãã ãã§ããå½ç¶ãªããã½ã¼ã¹ããã³ã³ãã¤ã«ãã Raspberry.IO.dll ãªã©ã¯ Raspberry Pi èªä½ã«ã³ãã¼ãã¦ããå¿ è¦ãããã¾ãã
mcs -r:Raspberry.IO.dll -r:Raspberry.IO.GeneralPurpose.dll -r:Raspberry.IO.Components.dll mcp3002.cs
ã³ã³ãã¤ã«ããå¾ã«å®è¡ãã㨠1 ç§ãã¨ã« Channel 0 / 1 ã®å¤ãåºåãã¦ããã¾ãã
ãã®æã«ã¯ Channel 0 ã«ç §åº¦ã»ã³ãµã¼ããChannel 1 ã«æ¸©åº¦ã»ã³ãµã¼ãä»ãã¦ãã£ãã®ã§ãæããã«ãã£ã¦ Channel 0 ã®æ¹ã ãå¤ã大ããå¤ãã£ã¦ã¾ãã
SPI å¨ãã«é¢ãã¦ã¯ Raspberry Pi 2 ã¨ã®äºææ§ã®åé¡ã¯çºçãã¦ããªãããã§ããã
追è¨
Raspberry# IO ã« Pull Request ãæãã¦ããåããã¼ã¸ããã¾ãããNuGet ãæ´æ°ãããã®ãå¾ ã¡ã¾ãã
å°ããã¤ã§ãããéçºèªä½ãé²ãã§ãããããªã®ã§ãä»å¾ã注ç®ãã¦ããããã§ããã