1

I'm trying to change values such that the arduino uno outputs comes from what is input, with the code,

int PinOutput = 11;
int PinInput = A0;
int inph;
int inpl;
int input;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(PinInput, INPUT);
pinMode(PinOutput, OUTPUT);
delay(100);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available())

  {

    input = Serial.read();         

  }

 else
{
 
    input = 255;

}
    inpl = analogRead(PinInput)/4;
    Serial.println(inpl);
    analogWrite(PinOutput,input);
    delay(10);
    inph = analogRead(PinInput)/4;
    Serial.println(inph);
    analogWrite(PinOutput,input/2);
    delay(10); 

}

Where pin 11 is connected with an RC circuit to pin A0. Will this code take a value through pin 11 and output it to pin A0? I'm checking if this should work right, so I can then check the python code that goes with it. As of now it mostly stalls, but maybe it's a problem with the python code. I'm trying to make it sustain the number put in. Python code I've tried with is,

import serial
import time

ser = serial.Serial('COM3', baudrate = 9600, timeout = 1)
time.sleep(1)



while 1:
    
    userInput = input('Get data point? ')

    ser.write(userInput.encode())

        
    for i in range(5):
        arduinooutput = float(str(ser.readline().decode().split('\r\n')[0].strip('\x00')))
        print(arduinooutput)    
        time.sleep(0.01)

The numbers printed out are somewhat random, around 100, it seems, no matter the value put in.

10
  • pin 13 is not a PWM pin on Uno. arduino.cc/reference/en/language/functions/analog-io/… – Juraj Aug 28 '20 at 16:30
  • @Juraj ok I switched it to 11 – user14094230 Aug 28 '20 at 17:00
  • PWM is fast switching between 0 V and HIGH. so analogRead will read only 0 or 1023. – Juraj Aug 28 '20 at 17:54
  • You will need a filter between the PWM output and he analog input. Use your favorite web search engine and look for "R/C filter". Since you are waiting for a second already, a long time constant will be fine, without calculating you might like to start with 100 kOhm and 1000 µFarad. – the busybee Aug 28 '20 at 18:35
  • @the busybee ok I can make an rc circuit. So is there no way to send a signal through one pin and receive it through another with a range of values (with just a wire connection)? Only 0 V or HIGH? – user14094230 Aug 28 '20 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.