Skip to content

Instantly share code, notes, and snippets.

@calcinai
Created September 6, 2017 23:51
Show Gist options
  • Save calcinai/40c6b9bf23cccdb3c1559fd5d2900fd0 to your computer and use it in GitHub Desktop.
Save calcinai/40c6b9bf23cccdb3c1559fd5d2900fd0 to your computer and use it in GitHub Desktop.
Example for MCP3008 with PHPi
<?php
include __DIR__.'/../vendor/autoload.php';
use Calcinai\PHPi\Peripheral\SPI;
use Calcinai\PHPi\Pin\PinFunction;
use Calcinai\PHPi\External\ADC\Microchip\MCP3008;
$board = \Calcinai\PHPi\Factory::create();
//Flip the appropriate pins to their alt functions
$board->getPin(10)->setFunction(PinFunction::SPI0_MOSI);
$board->getPin(9)->setFunction(PinFunction::SPI0_MISO);
$board->getPin(11)->setFunction(PinFunction::SPI0_SCLK);
$board->getPin(8)->setFunction(PinFunction::SPI0_CE0_N);
$adc = new MCP3008($board->getSPI(SPI::SPI0), 0);
$pin = $board->getPin(18);
//In 10-bit, will be between 0 and 1024
$value = $adc->getChannel(0)->read();
if($value > 500){
$pin->high();
} else {
$pin->low();
}
@IZ7BOJ
Copy link

IZ7BOJ commented Jun 15, 2018

Hi,
where can I find autoload.php?
Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment