This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

AC RMS Voltage Sensor Using STM32F103C8 and ZMPT101B

Hello all! this is a working (below at the end), code for using ZMPT to measure Ac mains 250VAC, The code is a working code and it is fairly accurate but my problem is that I have been trying quite for some time now to translate it to STM32 Keil uVision but I can't seem to do it. I want to start My ADC learning base on this. Please can someone here help me out to port this code to Keil uVision, I want the  RMS to display on serial monitor via UART1 TX_Only.

void setup(void) {

  Serial.begin(9600); // initialize serial communication at 9600 bits per second:
  //analogReference(INTERNAL);  // set ADC positive reference voltage to 1.1V (internal)
}
 
// get maximum reading value
uint16_t get_max() {
  uint16_t max_v = 0;
  for(uint8_t i = 0; i < 100; i++) {
    uint16_t r = analogRead(A0);  // read from analog channel 3 (A0)
    if(max_v < r) max_v = r;
    delayMicroseconds(200);
  }
  return max_v;
}
 
// main loop
void loop() {
 
  // get amplitude (maximum - or peak value)
  uint32_t v = get_max();
  // get actual voltage (ADC voltage reference = 1.1V)
  v = v * 5000/1023;
//  v = v * 5000/1023;  
  // calculate the RMS value ( = peak/√2 )
  v /= sqrt(2);
  v= v+2;
  uint32_t v2 = (v*1.414);
  // print out the value you read:
  Serial.print("AC Volt1 =  ");  
  Serial.print(v);
  Serial.print("   ");
  Serial.print("AC Volt2 =  ");  
  Serial.println(v2);
  
  //Serial.print(v);  
  //Serial.println("\r\n  "); 
  delay(500);

Sorry! In case you have viewed this code few minutes ago, I posted a wrong code but a have corrected it now.

ouble sensorValue1 = 0; double sensorValue2 = 0; int val[100]; int max_v = 0; double VmaxD = 0; double VeffD = 0; double RMS = 0; void setup() {  Serial.begin(9600); } void loop() {  for ( int i = 0; i 511) {  val[i] = sensorValue1;...Code I am learning STM32 with Keil but I am bewildered in some translations from my little knowlade