Site Tools


Sidebar

Content

pub:potentiomenter

Potentiometer

What is a potentiometer? The potentiometer is actually a variable resistor with three lead segments. The potentiometer is usually composed of a resistor body and a movable brush. When the brush moves along the resistor body, a resistance value or voltage that is related to the unique quantity is obtained at the output.

Microbit

Preparation

1 * microbit
1 * microbit expansion board
1 * usb cable
1 * Potentiomenter
-Several Jumper wires

Adeept potentiometer mudule

The potentiometer used in this experiment:

A Analog output
+ 3.3V
- GND


Now connect the potentiometer to the micro:bit

Connection diagram:


Sample code:
Makecode:


MU Python:

  #-----------------------------------------------------------
  # File name   : Potentiomenter.py
  # Description : Read the value of the potentiometer.
  # Author      : jason
  # E-mail      : jason@adeept.com
  # Website     : www.adeept.com
  # Date        : 2018/12/20
  #-----------------------------------------------------------
  from microbit import *
 
  while True:
      # define potentiometer pin
      potentiometer_reading = pin0.read_analog()
      display.scroll(str(potentiometer_reading))
      sleep(1000)
Display effect

When rotate the potentiometer, the analog value of it is displayed on the matrix LED.


Arduino

Preparation
1 * Adeept Arduino UNO R3 Board
1 * Potentiometer Module
1 * USB Cable
1 * 3-Pin Wires

Connection diagram:


arduino code:

  /***********************************************************
  File name: PotentiometerModule.ino
  Description: Now, when you turning the shaft of the potentiometer,
               you will see the data in the serial monitor 
  Website: www.adeept.com
  E-mail: support@adeept.com
  Author: Tom
  Date: 2018/23/20 
  ***********************************************************/
  int potentiometerPin = 0;        // potentiometer wiper (middle terminal) connected to analog pin 0
  void setup() 
  {                
    pinMode(potentiometerPin, INPUT);
    Serial.begin(9600);           // opens serial port, sets data rate to 9600 bps
  }
  void loop() 
  {
   Serial.println(analogRead(0)); //send data to the serial monitor
   delay(50);                      // delay 0.05 s
  }

Select Arduino development board and COM port


Upload the code to Arduino


Then open the derail port:


ASCII

Now when we rotate the potentiometer, we can see the data on the serial port.

Raspberry pi

Preparation

1 * Raspberry Pi
1 * GPIO Extension Board
1 * 40-Pin GPIO Cable
1 * Breadboard
1 * ADC0832 Module
1 * Potentiometer Module
2 * 3-Pin Wires
1 * 5-Pin Wires

Step1: Connect the module according to the connection diagram below


C code:

    /*
   * Website     : www.adeept.com
   * E-mail      : support@adeept.com
   * Author      : Jason
   * Date        : 2018/12/20
   */
  #include <wiringPi.h>
  #include <stdio.h>
  
  typedef unsigned char uchar;
  typedef unsigned int  uint;
 
  #define     ADC_CS    0
  #define     ADC_DIO   1
  #define     ADC_CLK   2
  
  uchar get_ADC_Result(void)
  {
  	//10:CH0
  	//11:CH1
  	uchar i;
  	uchar dat1=0, dat2=0;
 
  	digitalWrite(ADC_CS, 0);
 
  	digitalWrite(ADC_CLK,0);
  	digitalWrite(ADC_DIO,1);	delayMicroseconds(2);
  	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
  	digitalWrite(ADC_CLK,0);
 
  	digitalWrite(ADC_DIO,1);    delayMicroseconds(2); //CH0 10
  	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
  	digitalWrite(ADC_CLK,0);
 
  	digitalWrite(ADC_DIO,0);	delayMicroseconds(2); //CH0 0
 
  	digitalWrite(ADC_CLK,1);	
  	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
  	digitalWrite(ADC_CLK,0);	
  	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
 
  	for(i=0;i<8;i++)
  	{
  		digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
  		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);
 
  		pinMode(ADC_DIO, INPUT);
  		dat1=dat1<<1 | digitalRead(ADC_DIO);
  	}
 
  	for(i=0;i<8;i++)
  	{
  		dat2 = dat2 | ((uchar)(digitalRead(ADC_DIO))<<i);
  		digitalWrite(ADC_CLK,1); 	delayMicroseconds(2);
  		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);
  	}
 
  	digitalWrite(ADC_CS,1);
 
  	pinMode(ADC_DIO, OUTPUT);
 
  	return(dat1==dat2) ? dat1 : 0;
  }
 
  int main(void)
  {
  	uchar adcVal;
  	float vol;
 
  	if(wiringPiSetup() == -1){
  		printf("setup wiringPi failed !");
  		return 1; 
  	}
 
  	pinMode(ADC_CS,  OUTPUT);
  	pinMode(ADC_CLK, OUTPUT);
 
  	while(1){
  		pinMode(ADC_DIO, OUTPUT);
  		adcVal = get_ADC_Result();
  		vol = 3.3/255 * adcVal;
  		printf("analog value: %03d   voltage: %.2fV\n", adcVal, vol);
 
  		delay(100);
  	}
 
  	return 0;
  }

Python code:

  #!/usr/bin/env python
  import ADC0832
  import time
 
  def init():
  	ADC0832.setup()
 
  def loop():
  	while True:
  		res = ADC0832.getResult()
  		vol = 5.0/255 * res
  		print 'analog value: %03d  ||  voltage: %.2fV' %(res, vol)
  		time.sleep(0.2)
 
  if __name__ == '__main__':
  	init()
  	try:
  		loop()
  	except KeyboardInterrupt: 
  		ADC0832.destroy()
  		print 'The end !'
 

For C language users:

Step 2: Edit and save the code with vim or nano.

(Go to the folder where the code is stored)

Step 3: Compile

$ sudo gcc potentiometer.c -o potentiometer -lwiringPi

Step 4: Run

$ sudo ./potentiometer

For Python users:

Step 2: Edit and save the code with vim or nano.

Step 3: Run

$ sudo python potentiometer.py

Turn the knob on the Potentiometer module, you will find that the value displayed on the terminal is changed.



Data code:potentiomenter_module.rar
Video link:http://www.adeept.com/video/

pub/potentiomenter.txt · Last modified: 2018/12/28 12:11 by Adeept