User Tools

Site Tools


analog-in_digital-out_potentiometer_and_leds

This is an old revision of the document!


Analog-In/Digital-Out (Potentiometer and LEDs)

Intro

HiTechnic's Prototype Board allows analog input and digital input and output to be easily achieved with a breadboard for circuit prototyping and a LEGO NXT from the Mindstorms robotics kit. This board connects to the standard NXT sensor port and communicates over an I2C serial bus.<br> <br> The files to accompany the HiTechnic Prototype Board (old version, not “SuperPro”) can be found on [http://www.hitechnic.com/products HiTechnic'c Downloads page] or right here:<br> HiTechnic_ExperimentersKitA.zip <br> The PDF handbook included in the file above can also be downloaded here:<br> HiTechnicExperimentersKitHandbookA.pdf

Wiring

{|style=“ border-collapse: separate; border-spacing: 15; border-width: 0px; border-style: solid; border-color: #FFFFFF; padding: 0px; text-align: center;”

!style=“width: 75px; background: #F0F0F0”| Part !style=“width: 100px; background: #F0F0F0”| Quantity

<br> The six LEDs are connected to six pins that will act as a digital out. The potentiometer's variable resistance causes a variable voltage to be read at the analog input AO. This voltage is read in as a number between 0 and 1024. The LEDs are lit up linearly as the potentiometer is rotated.<br> For example, when the pin A0 reads a voltage corresponding to 200 (~20%), the first LED will light up. At 400, 2 LEDS will light. At 1000 or greater, all LEDs will be lit. This digital-out pin control can be realized in the code excerpt below:<br><br>

<source lang=“c” line start=“80”> … if(inputdata>200) outputdata=b0|b1; if(inputdata>400) outputdata=b0|b1|b2; if(inputdata>600) outputdata=b0|b1|b2|b3; if(inputdata>800) outputdata=b0|b1|b2|b3|b4; if(inputdata>1000) outputdata=b0|b1|b2|b3|b4|b5; … </source>

<br><br>

{| style=“text-align: center; margin: 1em auto 1em auto;”

{|

style=“border: 1px solid #CCCCCC;” valign=“center”
style=“width: 10px”
style=“border: 1px solid #CCCCCC;” valign=“center”
style=“border: 1px solid #CCCCCC; background: #F9F9F9”
style=“border: 1px solid #CCCCCC; background: #F9F9F9”
Video

<br> 'Note:' If the video doesn't work just reload the page.

NXC Code

<source lang=“c” line start=“1”>

#include “NXCDefs.h”

#define PROTO_PORT IN_1

int inputdata; int outputdata; int count; byte cmndbuf[]; buffer for outbound I2C command byte respbuf[]; buffer for inbound I2C response

void readdata()

{
ArrayInit(cmndbuf, 0, 2);     // set the buffer to hold 2 values
cmndbuf[0] = 0x02;            // set write to channel
cmndbuf[1] = 0x42;            // to set read address
count=2;                      // 2 bytes to read
I2CBytes(PROTO_PORT, cmndbuf, count, respbuf);  // issue I2C write command and read the byte back
inputdata=respbuf[0]*4+respbuf[1];              // create input value by reading the high order byte,
                                                // shift it left 3 bits and add the low order byt to
                                                //create a full 10 bit value
}

void writedata()

{
ArrayInit(cmndbuf, 0, 3);     // set the buffer to hold 3 values
cmndbuf[0] = 0x02;            // set write to channel
cmndbuf[1] = 0x4D;            // to set write address
cmndbuf[2] = outputdata;      // to set write data
count=0;                      // no bytes to read
I2CBytes(PROTO_PORT, cmndbuf, count, respbuf);  // issue I2C write command and read the byte back
}

task main()

{

byte b0 = 1;
byte b1 = 2;
byte b2 = 4;
byte b3 = 8;
byte b4 = 16;
byte b5 = 32;

SetSensorLowspeed(PROTO_PORT); // set sensor port 1 to low speed serial (I2C)
Wait(100);

ArrayInit(cmndbuf, 0, 3);     // set the buffer to hold 3 values
cmndbuf[0] = 0x02;            // set write to channel
cmndbuf[1] = 0x4E;            // to set write address
cmndbuf[2] = 0x3F;            // to write 111111
count=0;                      // no bytes to read
I2CBytes(PROTO_PORT, cmndbuf, count, respbuf);  // issue I2C write command
Wait(100);

while (TRUE)
  {
  readdata();                   // read the analog port 0
  ClearScreen();
  NumOut(20, LCD_LINE1, inputdata);
  outputdata=b0;                      //set the output value to turn one LED on
  if(inputdata>200)  outputdata=b0|b1;     //       based on the input value
  if(inputdata>400)  outputdata=b0|b1|b2;
  if(inputdata>600)  outputdata=b0|b1|b2|b3;
  if(inputdata>800)  outputdata=b0|b1|b2|b3|b4;
  if(inputdata>1000) outputdata=b0|b1|b2|b3|b4|b5;
  writedata();
  Wait(50);
  }

} </source>

Download

analog-in_digital-out_potentiometer_and_leds.1478040162.txt.gz · Last modified: 2016/11/01 15:42 by dwallace