PHP code example of volantus / mcp3008

1. Go to this page and download the library: Download volantus/mcp3008 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

volantus / mcp3008 example snippets


use Volantus\MCP3008\Reader;
use Volantus\Pigpio\Client;
use Volantus\Pigpio\SPI\RegularSpiDevice;

$spiInterface = new RegularSpiDevice(new Client(), 1, 32000, 0);
$reader = new Reader($spiInterface, 3.3);

use Volantus\MCP3008\Reader;
use Volantus\Pigpio\Client;
use Volantus\Pigpio\SPI\BitBangingSpiDevice;

$spiInterface = new BitBangingSpiDevice(new Client(), 12, 16, 20, 21, 32000);
$reader = new Reader($spiInterface, 3.3);

use Volantus\BerrySpi\RegularInterface;
use Volantus\MCP3008\Reader;

$spiInterface = new RegularInterface(1, 32000, 0);
$reader = new Reader($spiInterface, 3.3);

use Volantus\BerrySpi\BitBangingInterface;
use Volantus\MCP3008\Reader;

$spiInterface = new BitBangingInterface(12, 16, 20, 21, 32000, 0);
$reader = new Reader($spiInterface, 3.3);

// Reading value of ADC channel 4
$value = $reader->read(4);

// Getting the raw value, e.g. 789
$value->getRawValue();

// Getting the calculated voltage depending on reference voltage
// e.g. 2.54V in case of 3.3V ref. voltage 
$value->calculateVoltage();

$spiInterface = new RegularInterface(1, 32000, 0);
$spiInterface->open();

$reader = new Reader($spiInterface, 3.3);

$spiInterface->close();
unset($reader);