Using Sensors

One of the most useful tools you have available to you are sensors. There are two types of sensors:

Prebuilt/Preset/Kit Sensors

These sensors are useful little tidbits made by Lego, Hitechnic, or Modern Robotics. They generally come with the kit. LEGO sensors include the ultrasonic and touch sensors. HiTechnic sensors include the Infrared Sensor and the Gyro/Acceleration sensors. Modern Robotics sensors are new – they overlap very much on older sensors, and generally on the Lancers we don’t use them because they are very unreliable and extremely expensive.

If you have any of these sensors, you can look through the APIs for a way to use them. As of the date this post was written, the Gyro, Color, Ultrasonic, and Infrared Sensors have been documented.


Third Party Sensors

With the Device Interface Module, we can use 3rd party sensors. These are relatively cheap (most are $3-20 and some are high end and $70, but none like the extremely expensive Modern Robotics sensors) and very reliable. To use them, you must first know your way around the Device Interface Module – we’ll call it the DIM.

First, upon picking it up, you’ll notice it has three rows of ports. You can find the specifications of ports here in the middle download for the PDF of the core device interface module specifications.

You can interface sensors in four ways:

  • I2C – very difficult and complex, but can be a versatile tool. Many sensors use this, including the IMU from Adafruit.
  • Digital Output/Input – you can use the digital input/output to read/send values to sensors.
  • Analog Input/Output – you can use these to send A/D or read a variety of values from 0 to 1023.
  • PWM – using Pulses you can communicate with PWM enabled devices.

You can use a variety of methods to interface these devices, all of which can be found here in the hosted API.


dim.setDigitalChannelMode(0,Mode.INPUT); //dim = the DIMsleep(3);telemetry.addData("Sensor on D0",dim.getDigitalChannelState(0)); //log the value of the sensor

This sets digital channel 0 to input (you have to define whether you want to output or get input from a channel before using it). Then, it reads the bit from the channel and logs it to the telemetry. Using this you can get values from a digital sensor, such as a simple touch sensor.
Coming Soon: As the season progresses, we will add our own code for sensors here.

Skip to toolbar